简体   繁体   English

滚动到另一个DIV时更改CSS

[英]change css of one DIV when scrolled over another

I'm trying to get this to work with no avail. 我试图使它工作无济于事。 I am trying to get a menu item's CSS to change when I am scrolling past a specific DIV. 当我滚动经过特定的DIV时,我试图更改菜单项的CSS。

In this case, I am trying to get a menu item (#menu-item-3907) to change it's background color when I am scrolling over the div labelled #section-1. 在这种情况下,当我在标有#section-1的div上滚动时,我试图获取一个菜单项(#menu-item-3907)来更改其背景颜色。

This is the code I've tried so far: 到目前为止,这是我尝试过的代码:

var t = $("#section-1").offset().top;

$(document).scroll(function(){
if($(this).scrollTop() > t)
{   
    $('#menu-item-3907').css({"display":"none"});
}
});
var t = $("#section-1").scrollTop();

$(document).scroll(function(){
    if($(this).scrollTop() > t){
        $('#menu-item-3907').css({"background":"yellow"});
    }
});

Tried using HTML: 尝试使用HTML:

<div id="section-1">Section 1</div>
<div id="menu-item-3907" style="margin-top:1500px">Menu</div>

Try this. 尝试这个。

    <script>
$(window).scroll(function() {

    var scroll_pos = window.pageYOffset;



    if(scroll_pos>=300) {
       $("#menu-item-3907").css("background-color","#B44DFE");
    }
    else
    {
        $("#menu-item-3907").css("background-color","#FFF");
    }
}); 

    </script>
<div id="#section-1" style="color:pink;height:300px;border:1px solid">
hi

  </div>  

<div id="menu-item-3907"  style="color:blue;height:300px;border:1px solid">

hi
</div>

http://jsfiddle.net/LnWhJ/1/ http://jsfiddle.net/LnWhJ/1/

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

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