gpt4 book ai didi

javascript - 使用 jQuery 将固定位置添加到 div

转载 作者:行者123 更新时间:2023-11-28 10:58:04 25 4
gpt4 key购买 nike

我正在尝试实现 div 滚动直到到达顶部并停留在那里的效果。

我是通过以下方法实现的:

HTML

<div id="nav">this is nav</div>
<div id="mooey">
<div id="theFixed" style="position:fixed; background-color:red">SOMETHING</div>
</div>

CSS

#mooey {
background: green;
min-height:250px;
margin-top:300px;

#nav {
background:#000000;
position:fixed;
top:0;
width:100%;
height:100px;
}

JavaScript

$(window).scroll(function(){
$("#theFixed").css("top", Math.max(100, 300 - $(this).scrollTop()));
});

我想做什么,而不是声明 div theFixed 在 HTML 的样式中是固定的。我想知道是否有一种使用代码来应用它的方法。

原因是,如果脚本因任何原因未启用或失败 - 我希望 theFixed div 与 mooey div 一起滚动而不是被卡住在页面中间。

你可以在这里看到我做了什么:

http://jsfiddle.net/susannalarsen/4J5aj/7/

有什么想法吗?

最佳答案

使用 $('#theFixed').css('position','fixed'); 固定它。

关于javascript - 使用 jQuery 将固定位置添加到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22562616/

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