gpt4 book ai didi

javascript - 向下滚动时将类添加到下一个元素,并从父元素中删除类

转载 作者:行者123 更新时间:2023-11-28 04:28:13 26 4
gpt4 key购买 nike

我想知道 this website 上的侧边导航如何?制成?因此,当您向下滚动时,下一个元素会突出显示,而前一个元素会不亮。在我的例子中,我想,当我向下滚动时,下一个元素获得一个 current 类,并且到前一个元素,该类被删除。当它到达末尾并向下滚动时,它会再次从第一个元素开始。

这是我的片段:

* {
box-sizing: border-box;
}
body {
background: #000;
}
*,
html,
body {
margin: 0;
padding: 0;
}
.line-container {
width: 100px;
display: -webkit-flex;
display: -moz-flex;
display: -ms-flex;
display: -o-flex;
display: flex;
-webkit-flex-direction: column;
-moz-flex-direction: column;
-ms-flex-direction: column;
-o-flex-direction: column;
flex-direction: column;
position: fixed;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.line {
width: 25px;
height: 5px;
background-color: #fff;
margin-bottom: 1.5em;
cursor: pointer;
transition: all 0.35s ease-in-out;
}
.current {
width: 45px;
background-color: #fff;
transition: all 0.35s ease-in-out;
}
<div class="line-container" id="container">
<div class="line current"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
<div class="line"></div>
</div>

最佳答案

您可以尝试使用 jQueryBootstrap JS 制作scroll-spy。查看本教程: http://www.w3schools.com/bootstrap/bootstrap_scrollspy.asp Scroll-spy 在您滚动到目标元素时添加类 active,即使没有点击导航。希望这会解决您的问题;)

附言还要检查 onscroll="yourFunction()" 属性的作用。

关于javascript - 向下滚动时将类添加到下一个元素,并从父元素中删除类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41860646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com