简体   繁体   English

从href移除兄弟姐妹类

[英]remove siblings class from a href

I'm using owl carousel on a menu. 我在菜单上使用猫头鹰传送带。 when I scroll to the div the owl carousel auto slide to the right slides. 当我滚动到div时,猫头鹰轮播会自动滑动到右侧幻灯片。 now when I arrived in the specific div I add class to the slide (active) but for some reason, I can't remove the active class from the other slides (his siblings). 现在,当我到达特定的div时,我向幻灯片中添加了班级(活动),但由于某种原因,我无法从其他幻灯片(他的兄弟姐妹)中移除活动的班级。

I think it will be best to check the jsfiddle to understand the problem... 我认为最好检查jsfiddle以了解问题...

<div class="body">
  <div class="menu">
    <ul class="owl-carousel owl-theme">
      <a href="#a" data-num="0">Review</a>
      <a href="#a" data-num="1" class="item">a</a>
      <a href="#b" data-num="2" class="item">b</a>
      <a href="#c" data-num="3" class="item">c</a>
      <a href="#d" data-num="4" class="item">d</a>
      <a href="#d" data-num="5" class="item">e</a>
      <a href="#d" data-num="6" class="item">f</a>
    </ul>
  </div>

JS file JS文件

$('.owl-carousel').owlCarousel({
    nav: false,
    dots: false,
    singleItem: true,
})
   var owl = $('.owl-carousel');
   owl.owlCarousel();

   $( window ).scroll(function() {
      let scrollbarLocation = $(this).scrollTop();

        let scrollLinks = $('.item');

        scrollLinks.each(function(){
            let sectionOffset = $(this.hash).offset().top;

            if (sectionOffset <= scrollbarLocation){
                $(this).siblings().removeClass('active-link');
                $(this).addClass('active-link');

                let goToSlide = $(this).attr('data-num')
                owl.trigger('to.owl.carousel', goToSlide);
            }
        })

        if( scrollbarLocation === 0){
            scrollLinks.removeClass('active-link');
            owl.trigger('to.owl.carousel', 0);
        }
    });

check https://jsfiddle.net/jt31h4pr/132/ 检查https://jsfiddle.net/jt31h4pr/132/

The problem is that you remove/add active-link class on the same element ( this ). 问题是您在同一元素(this)上删除/添加了active-link类。 You need to removeClass only on the the element that already has class active-link . 您只需要在具有class active-link类的元素上移除removeClass。 The class active is controlled by the plugin and all the elements that are visible have the active class active类由插件控制,所有可见元素都具有active

See below 见下文

 $('.owl-carousel').owlCarousel({ nav: false, dots: false, singleItem: true, }) var owl = $('.owl-carousel'); owl.owlCarousel(); $( window ).scroll(function() { let scrollbarLocation = $(this).scrollTop(); let scrollLinks = $('.item'); scrollLinks.each(function(){ let sectionOffset = $(this.hash).offset().top; if (sectionOffset <= scrollbarLocation){ $('.active-link').removeClass('active-link'); // added $(this).addClass('active-link'); let goToSlide = $(this).attr('data-num') owl.trigger('to.owl.carousel', goToSlide); } }) if( scrollbarLocation === 0){ scrollLinks.removeClass('active-link'); owl.trigger('to.owl.carousel', 0); } }); 
 .body { height: 5000px; } ul { padding: 0; margin: 0; list-style-type: none; } .item { width: 200px; height: 70px; background: red; margin: 0 15px; color: #fff; display: flex; justify-content: center; align-items: center; border-bottom: 4px solid transparent; } .active-link { border-bottom: 4px solid #000; } .menu { position: fixed; top: 0; } section { width: 100%; height: 600px; background: #f8f9fb; } #a { background: lightblue; margin-top: 200px; } #b { background: lightgreen; } #c { background: tomato; } #d { background: lightpink; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css"> <div class="body"> <div class="menu"> <ul class="owl-carousel owl-theme"> <a href="#a" data-num="0">Review</a> <a href="#a" data-num="1" class="item">a</a> <a href="#b" data-num="2" class="item">b</a> <a href="#c" data-num="3" class="item">c</a> <a href="#d" data-num="4" class="item">d</a> <a href="#d" data-num="5" class="item">e</a> <a href="#d" data-num="6" class="item">f</a> </ul> </div> <section id="a"></section> <section id="b"></section> <section id="c"></section> <section id="d"></section> </div> 

Look into this fiddle 看看这个小提琴

https://jsfiddle.net/09sLpuwd/1/ https://jsfiddle.net/09sLpuwd/1/

What are you doing wrong is trying to remove active class from this and then add active class to the same this , which actualy do nothing. 什么是你做错了,是试图从删除活动类this ,然后添加活动类相同的this ,这actualy什么也不做。 Insted what I propose is to remove active class from all '.item' emelements and than add it to active one. 我建议的是从所有'.item'元素中删除活动类,然后将其添加到活动类中。

As alternative aproach you can store previous item and remove class from it, but I think first way is better. 作为替代方法,您可以存储以前的项目并从中删除类,但我认为第一种方法更好。

Hi the problem is with below line 您好问题是与下面的线

$(this).siblings().removeClass('active-link');

replace it with 替换为

$("div.active a").removeClass('active-link');

This was removing and adding class on same time on all so what i did is once i am removing the class from all i am adding it on present elemnt 这是在同一时间删除并添加类,所以一旦我从所有类中删除该类,我要做的就是在当前元素上添加它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM