且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用户滚动时移动图像或元素

更新时间:2022-03-30 00:27:42

Stack: Determine the direction of scroll

使用此功能,然后可以使用.animate

By using this, you can then use .animate

JSFiddle

var lastScrollTop = 0;
$("div").scroll(function (event) {
var st = $(this).scrollTop();
if (st > lastScrollTop) {
    $('img').animate({top: '-=10'}, 10);
} else {
    $('img').animate({top: '+=10'}, 10);
}
lastScrollTop = st;
});

忽略::如果您希望图像随页面一起移动,请将position: fixed;添加到CSS.

Disregard: If you would like the image to move with the page, add position: fixed; to the CSS.