简体   繁体   English

使用jQuery在基于另一个类的另一个div中添加一个类

[英]Adding a class in a another div based on another class using jQuery

I am trying to remove a div class when another div has the class 'scroll-to-fixed-fixed' using jQuery. 当另一个div使用jQuery将类“滚动固定修复”时,我正在尝试删除div类。 The scroll function works fine and the sticky header appears as desired. 滚动功能工作正常,并根据需要显示粘性标题。 but its the condition that doesn't seem to work. 但其条件似乎不起作用。 I have tried the following code but no luck. 我尝试了以下代码,但没有运气。 Kindly assist: : 请协助:

 $(window).scroll(function() { var scroll = $(window).scrollTop(); var y = $(this).scrollTop(); if (y > 10) { //clearHeader, not clearheader - caps H $(".scroll-nav-holder").addClass("scroll-to-fixed-fixed"); } else { $(".scroll-nav-holder").removeClass("scroll-to-fixed-fixed"); } }); if ($('.scroll-nav-holder').hasClass('scroll-to-fixed-fixed')) { $(".logo-holder").removeClass('nodisplay'); } else { $(".logo-holder").addClass('nodisplay'); } 
 .logo-holder { height: 100px; background: red; } .nodisplay { display: none; } .scroll-nav-holder { height: 20px; background: blue; } .container { height: 200px; background: green; } .scroll-to-fixed-fixed { position: fixed; top: 0px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="logo-holder nodisplay">something here</div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="scroll-nav-holder"> somethin more here </div> 

As mentioned above you should include you .logo-header condition inside scroll event. 如上所述,您应该在scroll事件中包含.logo-header条件。 Also, I slightly refactored your code using toggleClass function to make it a bit cleaner: 另外,我使用toggleClass函数稍微重构了您的代码,使其更加简洁:

 $(window).scroll(function() { var scroll = $(window).scrollTop(); // ? const toggleCondition = $(this).scrollTop() > 10; $(".scroll-nav-holder").toggleClass('scroll-to-fixed-fixed', toggleCondition); $(".logo-holder").toggleClass('nodisplay', toggleCondition); }); 
 .logo-holder { height: 100px; background: red; } .nodisplay { display: none; } .scroll-nav-holder { height: 20px; background: blue; } .container { height: 200px; background: green; } .scroll-to-fixed-fixed { position: fixed; top: 0px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="logo-holder nodisplay">something here</div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="container"> More stuff </div> <div class="scroll-nav-holder"> somethin more here </div> 

Just put it inside the if else statements where your also change the class of the "scroll-nav-holder" like so: 只需将其放在if else语句中,即可在其中更改“ scroll-nav-holder”的类,如下所示:

 $(window).scroll(function() { var scroll = $(window).scrollTop(); var y = $(this).scrollTop(); if (y > 10) { //clearHeader, not clearheader - caps H $(".scroll-nav-holder").addClass("scroll-to-fixed-fixed"); $(".logo-holder").removeClass('nodisplay'); } else { $(".scroll-nav-holder").removeClass("scroll-to-fixed-fixed"); $(".logo-holder").addClass('nodisplay'); } }); 

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

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