这是一个使用我自己的 jQuery 动画的实现。
HTML
<a href="#myDestinationAnchor" class="someClassName">Location</a>
JavaScript
// When the user clicks on an anchor with a class name "someClassName"...
$('body').on('click', 'a.someClassName', function(e) {
// Get the hash. In this example, "#myDestinationAnchor".
var targetSelector = this.hash;
var $target = $(targetSelector);
// Animate the scroll to the destination...
$('html, body').animate(
{
scrollTop: $target.offset().top // Scroll to this location.
}, {
// Set the duration long enough to allow time
// to lazy load the elements.
duration: 2000,
// At each animation step, check whether the target has moved.
step: function( now, fx ) {
// Where is the target now located on the page?
// i.e. its location will change as images etc. are lazy loaded
var newOffset = $target.offset().top;
// If where we were originally planning to scroll to is not
// the same as the new offset (newOffset) then change where
// the animation is scrolling to (fx.end).
if(fx.end !== newOffset)
fx.end = newOffset;
}
}
);
});
jQuery Animate的参考。
PS 我正在使用jQuery Lazy插件进行延迟加载。对这个插件没有特别的依赖(我知道),但我想我会提到它。