如何更改叠加粘性元素的不透明度?

huangapple go评论84阅读模式
英文:

How to change the opacity from overlaying sticky elements?

问题

我是编程新手,尤其是对JavaScript不太了解,遇到了一个问题,我就是无法解决。我网站右侧有几个元素(a,b,c,d,e,f,g)。它们是粘性的,都停在同一个位置,就像我想要的一样。

然而,当它们互相重叠时,我希望下面的元素的不透明度变为0。因此,我正在使用JavaScript,但我的代码根本不起作用。

我将我的JavaScript代码嵌入到HTML文档中,使用<script type="text/javascript"></script>

有人知道我如何解决这个问题吗?

谢谢你的帮助!

document.addEventListener('DOMContentLoaded', function() {
  var stickyElements = Array.from(document.getElementsByClassName('float'));

  function toggleStickyElements() {
    var scrollPosition = window.pageYOffset || document.documentElement.scrollTop;

    stickyElements.forEach(function(element, index) {
      var nextElement = stickyElements[index + 1];
      var currentMarginTop = element.offsetTop;
      var nextMarginTop = nextElement ? nextElement.offsetTop : Infinity;

      if (scrollPosition >= currentMarginTop && scrollPosition < nextMarginTop) {
        element.classList.add('hidden');
      } else {
        element.classList.remove('hidden');
      }
    });
  }

  window.addEventListener('scroll', toggleStickyElements);
  toggleStickyElements(); // 页面加载时的初始检查
});
.float {
  float: right;
  margin-top: calc(100vw / 7.51);
  margin-right: calc(100vw / 19.8);
  font-family: classic_roman_stdlight;
  font-size: calc(100vw / 25);
  color: hsl(228, 89%, 93%);
  opacity: 1;
  z-index: -6;
}

.hidden {
  display: none;
}

#alpha {
  position: sticky;
  top: calc(100vw / 7.51);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#beta {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#gamma {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#delta {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#epsilon {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#zeta {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#eta {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}

#A {
  margin-top: calc(100vw / 1.72);
  position: sticky;
  top: calc(100vw / 7.3);
  color: white;
}
<section>
  <title>Sticky Elements Example</title>
  <ol class="float">
    <li id="alpha"> α </li>
    <li id="beta"> β </li>
    <li id="gamma"> γ </li>
    <li id="delta"> δ </li>
    <li id="epsilon"> ε </li>
    <li id="zeta"> ζ </li>
    <li id="eta"> η </li>
    <li id="A"> b </li>
  </ol>
</section>
英文:

I'm new to coding, especially JavaScript, and ran into a problem that I just can't solve. I have a few elements (a,b,c,d,e,f,g) on the right side of my website. They are sticky and all stop at the same position. Just like I want them to.

However, when they overlay each other I want the element below to change its opacity to 0. Therefore I'm using JavaScript but my code isn't working at all.

I embedded my JavaScript-code into my html document using &lt;script type=&quot;text/javascript&quot;&gt; &lt;/script&gt;.

Does anyone know how I can fix the issue?

Thanks for your help!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.addEventListener(&#39;DOMContentLoaded&#39;, function() {
var stickyElements = Array.from(document.getElementsByClassName(&#39;float&#39;));
function toggleStickyElements() {
var scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
stickyElements.forEach(function(element, index) {
var nextElement = stickyElements[index + 1];
var currentMarginTop = element.offsetTop;
var nextMarginTop = nextElement ? nextElement.offsetTop : Infinity;
if (scrollPosition &gt;= currentMarginTop &amp;&amp; scrollPosition &lt; nextMarginTop) {
element.classList.add(&#39;hidden&#39;);
} else {
element.classList.remove(&#39;hidden&#39;);
}
});
}
window.addEventListener(&#39;scroll&#39;, toggleStickyElements);
toggleStickyElements(); // Initial check on page load
});

<!-- language: lang-css -->

.float {
float: right;
margin-top: calc(100vw / 7.51);
margin-right: calc(100vw / 19.8);
font-family: classic_roman_stdlight;
font-size: calc(100vw / 25);
color: hsl(228, 89%, 93%);
opacity: 1;
z-index: -6;
}
.hidden {
display: none;
}
#alpha {
position: sticky;
top: calc(100vw / 7.51);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#beta {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#gamma {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#delta {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#epsilon {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#zeta {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#eta {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.12);
}
#A {
margin-top: calc(100vw / 1.72);
position: sticky;
top: calc(100vw / 7.3);
color: white;
}

<!-- language: lang-html -->

&lt;section&gt;
&lt;title&gt;Sticky Elements Example&lt;/title&gt;
&lt;ol class=&quot;float&quot;&gt;
&lt;li id=&quot;alpha&quot;&gt; α &lt;/li&gt;
&lt;li id=&quot;beta&quot;&gt; β &lt;/li&gt;
&lt;li id=&quot;gamma&quot;&gt; γ &lt;/li&gt;
&lt;li id=&quot;delta&quot;&gt; δ &lt;/li&gt;
&lt;li id=&quot;epsilon&quot;&gt; ε &lt;/li&gt;
&lt;li id=&quot;zeta&quot;&gt; ζ &lt;/li&gt;
&lt;li id=&quot;eta&quot;&gt; η &lt;/li&gt;
&lt;li id=&quot;A&quot;&gt; b &lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;

<!-- end snippet -->

答案1

得分: 1

It seems like your code is not working because you're trying to hide the entire list, not individual list items. In your JavaScript, you are creating an array of elements with the class 'float', but there is only one element with this class: the entire ordered list (ol). Therefore, you're trying to hide the entire list instead of each list item individually.

英文:

It seems like your code is not working because you're trying to hide the entire list, not individual list items. In your JavaScript, you are creating an array of elements with the class 'float', but there is only one element with this class: the entire ordered list (ol). Therefore, you're trying to hide the entire list instead of each list item individually.

huangapple
  • 本文由 发表于 2023年6月29日 18:12:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定