Maison >interface Web >tutoriel CSS >Comment créer un Div Stick défilant après l'avoir passé ?
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/).
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!