Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make content sticky on scroll to a point in jQuery

I have a div that's set to position: relative. It becomes fixed when the top of the window reaches it. The div lives in a container (blue in the example below) and I would like to set it back to relative when it reaches the bottom of its parent container (blue).

This is my code simplified and my Fiddle:

HTML:

<div class="green"></div>

<div class="blue">
  <div class="sticky">Sticky</div>
</div>

<div class="red"></div>

CSS:

.blue {
  background-color: blue;
  height: 500px;
}

.sticky {
  width: 200px;
  background-color: yellow;
  text-align: center;
  top: 0;
}

.red {
  background-color: red;
  height: 800px;
}

.green {
  background-color: green;
  height: 200px;
}

And the jQuery:

$(document).ready(function() {
  var stickyTop = $('.sticky').offset().top;

  $(window).scroll(function() {
    var windowTop = $(window).scrollTop();

    if (stickyTop < windowTop) {
      $('.sticky').css('position', 'fixed');
    } else {
      $('.sticky').css('position', 'relative');
    }
  });
});
like image 253
Julesfrog Avatar asked Jan 18 '16 00:01

Julesfrog


People also ask

How do you make an element sticky in HTML?

With the help of both position property relative and fixed , we can easily create an sticky element depending upon the scroll position. The element position stay relative until it reach to the given offset position – then it “sticks” in place (like position:fixed).

How to control the scroll limit of a sticky scroll box?

The limit would be pointed by any webpage element till it has to scroll down. We will provide a separate class (or ID) to our sticky-scroll box and then use jQuery to control the box with the help of that class. Lets say, our sticky division carry the class ‘ sticky-scroll-box ‘.

What is a sticky section on a website?

Sticky divisions on websites are used to drag attention of readers to a particular section. For example, you have a subscribe box that is floating down the page as the user scrolls through it’s browser’s window. Such a floating, dynamic sticky-scroll effect can be created using jQuery.

What is the difference between fixed and sticky elements in HTML?

Both work in a similar way to fixed the HTML element and maintain their position on the browser screen, even the user scrolls down or up to the page. But the sticky element bounded to its parent container whereas the fixed element isn’t confined to its parent.


1 Answers

Add following condition to your if statement:

$(".blue").height() + $(".blue").offset().top > windowTop

Your code should look like this:

$(document).ready(function() {
  var stickyTop = $('.sticky').offset().top;

  $(window).scroll(function() {
    var windowTop = $(window).scrollTop();
    if (stickyTop < windowTop && $(".blue").height() + $(".blue").offset().top - $(".sticky").height() > windowTop) {
      $('.sticky').css('position', 'fixed');
    } else {
      $('.sticky').css('position', 'relative');
    }
  });
});

See updated JSFiddle.

like image 62
Michał Perłakowski Avatar answered Oct 06 '22 18:10

Michał Perłakowski