Reputation: 2679
How to make a div fixed on detection of Scrolling by the Users.
Example: Right Sidebar of Facebook, it gets stuck when a certain scroll position is attained.
Upvotes: 7
Views: 23351
Reputation: 94
It seams you are looking for position: sticky;
.
Learn more: https://css-tricks.com/position-sticky-2/
Upvotes: 0
Reputation: 50767
Monitor whether or not we're scrolling.
if($(window).scrollTop() > 0){
//we're scrolling our position is greater than 0 from the top of the page.
$("#element").css({'position' : 'fixed'});
}
*EDIT
Do it without jQuery..
if(window.scrollTop() > 0){
document.getElementById('element').style.position="fixed";
}
Upvotes: 5
Reputation: 420
Not sure if this is what you mean?
But you can add the CSS-propery position: fixed; to it to make it appear on the sam place even after scrolling.
Upvotes: 1
Reputation: 46549
position:fixed
is the answer.
But you can always look at a website's source if you want to know how they do something. Very educational!
Upvotes: 8