Farzad Bayan Farzad Bayan - 1 year ago 108
CSS Question

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

I have a

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

add fixed class to the navigation

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

It's what I made, but not work

bottom = $('.bottom');
if ($(this).scrollTop() > bottom){

Answer Source

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

That should do it.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download