【发布时间】:2019-05-23 15:00:11
【问题描述】:
我有一个不错的 Waypoint.js 滚动列表,当您向下滚动并从目标(窗口)点击某个百分比时,它会更改字体样式并向列表项添加一个箭头。
现在它在向下的每个项目中添加箭头,并在向上的每个项目中删除它。
我想知道的是,是否有一种方法可以以某种方式说“一次只有一个”,这将允许元素更改,但一次只能更改一个(即:添加一个箭头(字体真棒),并且一次改变一种字体)。
本质上,“不要一次将此类添加到多个,只有在向下滚动时将其从前一个中删除时才添加它(我希望这是有道理的!)
我尝试将其更改为 toggleClass 并从 .siblings() 中删除类,这很有效,但因为这些是多个列表,所以它不起作用。每个 ul 中的最后一项保留箭头(我将在下面展示一个示例)。
在该网站上,您可以看到它使用正确的样式和箭头显示:如果您将此页面向下滚动到“我们的过程”部分,您会看到我正在处理的内容:slight_smile:
https://stable.stable-demos.com/what-we-do/
另请注意:我知道我可以使用兄弟 () 来完成这项工作,但这些列表必须分解为不同的 ul,因为我们需要将它们正确堆叠在一起以供移动设备使用。
感谢您的帮助!
jQuery(function($){
// WHAT WE DO - ACTIVATE LIST AS SCROLL BY CHANGE FONT
var continuousElements = document.getElementsByClassName("waypoint")
for (var i = 0; i < continuousElements.length; i++) {
new Waypoint({
element: continuousElements[i],
handler: function(direction) {
if (direction === "down") {
console.log(this.element.innerHTML + "down");
$(this.element).addClass("gray active");
$(this.element).removeClass("black");
} else if (direction === "up") {
console.log(this.element.innerHTML + "up");
$(this.element).addClass("black");
$(this.element).removeClass("gray active");
}
}, offset: "50%"
});
}
}); // End jQuery
/* What we do Highlight Text */
.black {
color: black!important;
}
.gray {
color: black!important;
}
.gray.active:before {
font-family: "Font Awesome 5 Free";
content: "\f061";
padding-right: 10px;
}
.c-logo img {
width: 80%;
}
.list-wrap div {
text-transform: uppercase;
padding: 8px 0;
text-align: center;
color: black;
font-family: 'space_grotesksemibold';
}
.list-wrap div.active,
.list-wrap div:hover {
color: black;
font-family: 'gt_sectra_fineblack_italic';
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>
<div style="min-height: 2000px;">
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Think.</div>
<div class="continuous-true waypoint">We Research.</div>
<div class="continuous-true waypoint">We Involve.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Stragegize.</div>
<div class="continuous-true waypoint">We Plan.</div>
<div class="continuous-true waypoint">We Analyze.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Ideate.</div>
<div class="continuous-true waypoint">We Design.</div>
<div class="continuous-true waypoint">We Create.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Test.</div>
<div class="continuous-true waypoint">We Adapt.</div>
<div class="continuous-true waypoint">We Execute.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Report.</div>
<div class="continuous-true waypoint">We Learn.</div>
<div class="continuous-true waypoint">We Improve.</div>
</div>
</div>
与兄弟姐妹():
jQuery(function($){
// WHAT WE DO - ACTIVATE LIST AS SCROLL BY CHANGE FONT
var continuousElements = document.getElementsByClassName("waypoint")
for (var i = 0; i < continuousElements.length; i++) {
new Waypoint({
element: continuousElements[i],
handler: function(direction) {
if (direction === "down") {
console.log(this.element.innerHTML + "down");
$(this.element).addClass("gray active").siblings().removeClass('active');;
$(this.element).removeClass("black");
} else if (direction === "up") {
console.log(this.element.innerHTML + "up");
$(this.element).addClass("black");
$(this.element).removeClass("gray active");
}
}, offset: "50%"
});
}
}); // End jQuery
/* What we do Highlight Text */
.black {
color: black!important;
}
.gray {
color: black!important;
}
.gray.active:before {
font-family: "Font Awesome 5 Free";
content: "\f061";
padding-right: 10px;
}
.c-logo img {
width: 80%;
}
.list-wrap div {
text-transform: uppercase;
padding: 8px 0;
text-align: center;
color: black;
font-family: 'space_grotesksemibold';
}
.list-wrap div.active,
.list-wrap div:hover {
color: black;
font-family: 'gt_sectra_fineblack_italic';
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.min.js"></script>
<div style="min-height: 2000px;">
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Think.</div>
<div class="continuous-true waypoint">We Research.</div>
<div class="continuous-true waypoint">We Involve.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Stragegize.</div>
<div class="continuous-true waypoint">We Plan.</div>
<div class="continuous-true waypoint">We Analyze.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Ideate.</div>
<div class="continuous-true waypoint">We Design.</div>
<div class="continuous-true waypoint">We Create.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Test.</div>
<div class="continuous-true waypoint">We Adapt.</div>
<div class="continuous-true waypoint">We Execute.</div>
</div>
<div id="listWrap" class="list-wrap">
<div class="continuous-true waypoint">We Report.</div>
<div class="continuous-true waypoint">We Learn.</div>
<div class="continuous-true waypoint">We Improve.</div>
</div>
</div>
【问题讨论】:
-
sn-p 中的代码一次添加/删除一个类元素。也许是因为它们靠得很近,很难看到,但如果你慢慢滚动它就可以了。
-
是的,我想我的意思是,我希望在您向下滚动时添加 .active 类而不是列表项 1、2、3、4、5,我会就像一次只有一个列表项具有 .active 类。例如,如果您向下滚动并且列表项 2 为 .active,则列表项 1 的 .active 类将被删除。希望这是有道理的。感谢您的观看!
标签: jquery html css jquery-waypoints