简体   繁体   中英

add a class when div over another div

I have this jquery code that adds a class to a div(#menu) after the user scrolled that far on a page. But I am searching for a way to change that code into adding that class when the div(#menu) is on top of another div with the class(.remove) and remove the class again when it's on a div with the class(.add). Here is my code:

jQuery(window).scroll(function(){
    var fromTopPx = 400; // distance to trigger
    var scrolledFromtop = jQuery(window).scrollTop();
    if(scrolledFromtop > fromTopPx){
        jQuery('#menu').addClass('scrolled');
    }else{
        jQuery('#menu').removeClass('scrolled');
    }
});

You need to find the position of the div that you want to "be on top of".

To do that you can use

$('.remove').offset().top;

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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