Need to keep a specific div stationary after scrolling past it? Here's how to achieve that using either CSS or jQuery.
CSS-Only Approach
As of recent advancements, CSS alone can accomplish this functionality. More details can be found here: [CSS-Only Fixed Div After Scrolling](https://stackoverflow.com/a/53832799/1482443).
jQuery Approach
If jQuery is acceptable, try this:
Define the initial position of the div:
var fixmeTop = $('.fixme').offset().top;
Add a scroll event listener:
$(window).scroll(function() {
Determine if the scrolled position has passed the initial div position:
if (currentScroll >= fixmeTop) {
Apply fixed positioning when scrolled past the div:
$('.fixme').css({ position: 'fixed', top: '0', left: '0' });
Reset the positioning when scrolled above the div:
} else { $('.fixme').css({ position: 'static' }); }
For a more comprehensive understanding, check out this live example: [jQuery Fixed Div After Scrolling](http://jsfiddle.net/5n5MA/2/).
The above is the detailed content of How to Make a Scrolling Div Stick After Passing it?. For more information, please follow other related articles on the PHP Chinese website!