我是jquery新手,想在页面向下滚动超过50次时让div悬浮在屏幕上方,请问如何实现?
我希望 div 始终是绝对的而不是固定的。
$(document).ready(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 50) {
$(".articlebutton").css("top", "0px"); //I want this value to change dynamically as the scrollbar moves down, so that the div stays on top of screen
} else {
$(".articlebutton").css("top", "-50px");
}
});
});
最佳答案
您可以将它设置为顶部 -100,因为它是 -50 并且滚动发生在 50 之后:
$(".articlebutton").css("top", ($(window).scrollTop()-100)+"px");
关于javascript - 如何在向下滚动时实现 float div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486943/