Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a div cling to top of screen if scrolled down past it [duplicate]

I have a div which, when my page is first loaded, is about 100px from the top (it holds some buttons etc. for the page).

When a user scrolls past it, I would like the div to "follow" the user in that it attaches to the top of the screen. When the user returns to the top of the page, I want it back in its original position.

Visualization - xxxxx is the div:  Default (page load)          User vertically scrolled well past it ---------                    --------- |       |                    |xxxxxxx| < after div reaches top of screen when |xxxxxxx|                    |       |   page is scrolled vertically, it stays |       |                    |       |   there ---------                    --------- 
like image 261
Alex Avatar asked May 25 '10 18:05

Alex


People also ask

How can I make a div stick to the top of the screen once it's been scrolled to in react?

Just replace #sticky_div's_name_here with the name of your div, i.e. if your div was <div id="example"> you would put #example { position: sticky; top: 0; } .

How do I keep my div on screen when scrolling?

Use position:fixed; and set the top:0;left:0;right:0;height:100px; and you should be able to have it "stick" to the top of the page.


2 Answers

The trick is that you have to set it as position:fixed, but only after the user has scrolled past it.

This is done with something like this, attaching a handler to the window.scroll event

   // Cache selectors outside callback for performance.     var $window = $(window),        $stickyEl = $('#the-sticky-div'),        elTop = $stickyEl.offset().top;     $window.scroll(function() {         $stickyEl.toggleClass('sticky', $window.scrollTop() > elTop);     }); 

This simply adds a sticky CSS class when the page has scrolled past it, and removes the class when it's back up.

And the CSS class looks like this

  #the-sticky-div.sticky {      position: fixed;      top: 0;   } 

EDIT- Modified code to cache jQuery objects, faster now.

like image 51
adamJLev Avatar answered Sep 27 '22 20:09

adamJLev


The trick to make infinity's answer work without the flickering is to put the scroll-check on another div then the one you want to have fixed.

Derived from the code viixii.com uses I ended up using this:

function sticky_relocate() {     var window_top = $(window).scrollTop();     var div_top = $('#sticky-anchor').offset().top;     if (window_top > div_top)         $('#sticky-element').addClass('sticky');     else         $('#sticky-element').removeClass('sticky'); }  $(function() {     $(window).scroll(sticky_relocate);     sticky_relocate(); }); 

This way the function is only called once the sticky-anchor is reached and thus won't be removing and adding the '.sticky' class on every scroll event.

Now it adds the sticky class when the sticky-anchor reaches the top and removes it once the sticky-anchor return into view.

Just place an empty div with a class acting like an anchor just above the element you want to have fixed.

Like so:

<div id="sticky-anchor"></div> <div id="sticky-element">Your sticky content</div> 

All credit for the code goes to viixii.com

like image 29
Rafvs Avatar answered Sep 27 '22 19:09

Rafvs