gpt4 book ai didi

javascript - 如何在向下滚动时实现 float div

转载 作者:行者123 更新时间:2023-11-30 12:57:38 31 4
gpt4 key购买 nike

我是jquery新手,想在页面向下滚动超过50次时让div悬浮在屏幕上方,请问如何实现?

我希望 div 始终是绝对的而不是固定的。

http://jsfiddle.net/8UCcY/

$(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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com