简体   繁体   English

jQuery:为什么我的脚本无法删除该类?

[英]jQuery: Why can't my script remove the class?

jQuery won't remove the class after scrolling jQuery在滚动后不会删除该类

This is my script: 这是我的脚本:

$(window).scroll(function(){
    if($(window).scrollTop() >= $("#white").offset().top -70) {
        $('.burger-menu').addClass('white');
    } else {
        $('.burger-menu').removeClass('white');
    }
});

$(window).scroll(function(){
    if($(window).scrollTop() >= $("#color-main").offset().top -70) {
        $('.burger-menu').addClass('color-main');
    } else {
        $('.burger-menu').removeClass('color-main');
    }
});

$(window).scroll(function(){
    if($(window).scrollTop() >= $("#yellow").offset().top -70) {
        $('.burger-menu').addClass('yellow');
    } else {
        $('.burger-menu').removeClass('yellow');
    }
});

This is my HTML: 这是我的HTML:

<section class="home-page" id="white">Blablabla</section>
<section class="wrap" id="color-main">Blablabla</section>
<section class="wrap" id="yellow">Blablabla</section>

but my <div> 但是我的<div>

<div class="burger-menu white color-main yellow">

still has the classes "white" and "color-main", which should be removed. 仍然具有“ white”和“ color-main”类,应将其删除。 :( :(

I made a codepen for you to see it. 我制作了一个Codepen供您查看。

There are 2 fixes I would recommed you try: 我建议您尝试2种修复:

  1. Don't do multiple .scroll(function(){...}) calls, they overwrite each other. 不要执行多个.scroll(function(){...})调用,它们会相互覆盖。
  2. Use $("body").scroll(function(){...}) , because that's the element you (usally) scroll in 使用$("body").scroll(function(){...}) ,因为这是您(通常)滚动到的元素

The below example uses only the IF statement and deletes the other classes. 下面的示例仅使用IF语句,并删除其他类。 Try this and see if you get the result you desire. 试试看,看看是否能获得想要的结果。 Instead of relying on an ELSE that could be broken by another scroll function else statement we simply remove all classes and only add the class you want. 无需依赖ELSE可能会被另一个滚动功能else语句破坏的方法,我们只需删除所有类并仅添加所需的类即可。

Example: 例:

   $(window).scroll(function () {
       if ($(window).scrollTop() >= $("#white").offset().top - 70) {
           $('.burger-menu').removeClass("color-main").removeClass("yellow").addClass('white');
       }
   });
   $(window).scroll(function () {
       if ($(window).scrollTop() >= $("#color-main").offset().top - 70) {
           $('.burger-menu').removeClass("white").removeClass("yellow").addClass('color-main');
       } 
   });
   $(window).scroll(function () {
       if ($(window).scrollTop() >= $("#yellow").offset().top - 70) {
           $('.burger-menu').removeClass("color-main").removeClass("white").addClass('yellow');
       } 
   });

I agree with those who suggest putting them all in a single .scroll() function. 我同意那些建议将它们全部放在单个.scroll()函数中的.scroll() This worked for me: 这为我工作:

(function($) {
  $(window).scroll(function () {
    if ($(window).scrollTop() >= $("#white").offset().top - 70) {
      $('.burger-menu').addClass('white');
    } else {
      $('.burger-menu').removeClass('white');
    }
    if ($(window).scrollTop() >= $("#color-main").offset().top - 70) {
      $('.burger-menu').addClass('color-main');
    } else {
      $('.burger-menu').removeClass('color-main');
    }
    if ($(window).scrollTop() >= $("#yellow").offset().top - 70) {
      $('.burger-menu').addClass('yellow');
    } else {
      $('.burger-menu').removeClass('yellow');
    }
  });
})(jQuery);

To have only one class active at a time, use removeClass to remove the others. 要一次只激活一个类,请使用removeClass删除其他类。 You can use a single scroll event handler, and rearrange the code to look for the last section first, that makes the code simpler: 您可以使用单个scroll事件处理程序,然后重新排列代码以首先查找最后一部分,这使代码更简单:

$(window).scroll(function(){

  var top = $(window).scrollTop() + 70;

  if (top >= $("#yellow").offset().top) {
    $('.burger-menu').removeClass('white color-main').addClass('yellow');
  } else if(top >= $("#color-main").offset().top) {
    $('.burger-menu').removeClass('white yellow').addClass('color-main');
  } else if (top >= $("#white").offset().top) {
    $('.burger-menu').removeClass('color-main yellow').addClass('white');
  } else {
    $('.burger-menu').removeClass('white color-main yellow');
  }

});

Demo: https://jsfiddle.net/Guffa/yka8nzt4/1/ 演示: https : //jsfiddle.net/Guffa/yka8nzt4/1/

Another way would be to determine the status for each class: 另一种方法是确定每个类的状态:

$(window).scroll(function(){

  var top = $(window).scrollTop() + 70;
  var white = top >= $("#white").offset().top;
  var main = top >= $("#color-main").offset().top;
  var yellow = top >= $("#yellow").offset().top;

  $('.burger-menu')
    .toggleClass('white', white && !main)
    .toggleClass('color-main', main && !yellow)
    .toggleClass('yellow', yellow);

});

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

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