如何一次完成向一个项目添加类(WayPoints)

时间:2019-05-23 15:00:12

标签: jquery html css jquery-waypoints

我有一个不错的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>

有兄弟姐妹():

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>

1 个答案:

答案 0 :(得分:0)

首先,您可以删除具有相同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

//希望对您有帮助