简体   繁体   English

当div超过另一个div时添加一个类

[英]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. 我有这个jquery代码,它在用户在页面上滚动了那么远之后将一个类添加到div(#menu)中。 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). 但是我正在寻找一种方法来将div(#menu)放在具有class(.remove)的另一个div上时,将该代码更改为添加该类,并在div与class(。)一起位于div上时再次删除该类。加)。 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". 您需要找到要“位于其上方”的div的位置。

To do that you can use 为此,您可以使用

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

Hope this helps. 希望这可以帮助。

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

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