gpt4 book ai didi

jquery - 如何完成一次为一个元素添加类(WayPoints)

转载 作者:行者123 更新时间:2023-11-27 23:58:09 26 4
gpt4 key购买 nike

我有一个很好的 Waypoint.js 滚动列表,当您向下滚动并从目标(窗口)点击一定百分比时,它会更改字体样式并向列表项添加一个箭头。

现在,它在向下的过程中为每个元素添加箭头,并在向上的过程中从每个元素中移除箭头。

我想知道的是,是否有一种方法可以以某种方式说“一次一个”,这将允许元素更改,但一次只能更改一个(即:添加一个箭头(字体很棒),一次更改一种字体)。

基本上,“不要一次将这个类添加到多个,只有在向下滚动时将它从前面的一个中删除时才添加它(我希望这是有道理的!)

我尝试将其更改为 toggleClass 并从 .siblings() 中删除类,这有点奏效,但因为这些是多个列表,所以它不起作用。每个 ul 中的最后一项保留箭头(我将在下面展示一个示例)。

在这个网站上,您可以看到它以正确的样式和箭头显示:如果您向下滚动此页面到我们的流程部分,您将看到我正在做的事情:slight_smile:

https://stable.stable-demos.com/what-we-do/

另请注意:我知道我可以使用 siblings() 来完成这项工作,除了这些列表必须分解为不同的 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>

与 sibling ():

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>

最佳答案

首先,您有多个具有相同 Id(listWrap) 的元素,您可以将其删除,然后将类名添加到您的父 div(.list-wrap-container)。

<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;" class='list-wrap-container'>
<div 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 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 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 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 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>

//js代码

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) {
// select the parent container
$(this.element).parent('.list-wrap-container').find('.gray.active').removeClass('gray active');

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

//希望对你有帮助

关于jquery - 如何完成一次为一个元素添加类(WayPoints),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278054/

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