最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add a class to a DIV when top of the window reach a specific element and remove it when not - Stack Overflow

programmeradmin1浏览0评论

I have a .navigation in the top of the wrapper. I want to add it a .fixed class, when the top of the window reached the .bottom DIV & remove this class when the top of the .bottom is in the window`s scope (it's a toggling between add and remove .fixed class).

<div id="wrapper">
    <div class="navigation">
        <!-- There are some list elements here -->
    </div>
    <div class="bottom"></div>
</div>

It's what I made, but not work

bottom     = $('.bottom');
$(window).scroll(function(){    
    if ($(this).scrollTop() > bottom){ 
        $('.navigation').addClass('fixed'); 
    }
    else{
        $('.navigation').removeClass('fixed');
    }
});

I have a .navigation in the top of the wrapper. I want to add it a .fixed class, when the top of the window reached the .bottom DIV & remove this class when the top of the .bottom is in the window`s scope (it's a toggling between add and remove .fixed class).

<div id="wrapper">
    <div class="navigation">
        <!-- There are some list elements here -->
    </div>
    <div class="bottom"></div>
</div>

It's what I made, but not work

bottom     = $('.bottom');
$(window).scroll(function(){    
    if ($(this).scrollTop() > bottom){ 
        $('.navigation').addClass('fixed'); 
    }
    else{
        $('.navigation').removeClass('fixed');
    }
});
Share Improve this question edited Dec 1, 2013 at 16:07 Farzad Bayan asked Dec 1, 2013 at 15:56 Farzad BayanFarzad Bayan 2512 gold badges4 silver badges13 bronze badges 3
  • 1 Visualising your requirement is nice, But where's your code.? – Rajaprabhu Aravindasamy Commented Dec 1, 2013 at 15:58
  • How is the .navigation being scrolled? – acdcjunior Commented Dec 1, 2013 at 16:06
  • @acdcjunior It's scrolled with the default browser scroller – Farzad Bayan Commented Dec 1, 2013 at 16:11
Add a ment  | 

1 Answer 1

Reset to default 5

var bottom = $('.bottom').offset().top;

That should do it.

This pares the offset from the top of the viewport to the window's scrollTop() instead of paring a whole element.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论