![](/img/trans.png)
[英]How to stick the bottom part of a div to top of the screen when scrolling down?
[英]Stick div on screen when user is scrolling
当用户滚动时,我想让我的div right
地粘在屏幕上。 这是一个示例情况: https : //jsfiddle.net/gwjehuzf/
我尝试了很多,但没有成功。 像这样的东西:
$(window).scroll(function () {
if ($(document).scrollTop() > 400) {
console.log("a");
var newPos = $(document).scrollTop() + 400;
$('.right').css({ top: newPos });
}
else {
$('.right').css({ top: 400 });
}
})
由于某种原因不起作用。 有任何想法吗?
只需使用css .right {position:sticky; 顶部:0; 溢出:隐藏;}
只需添加几个CSS ,我们就可以解决您的问题。 谢谢
.right {
position: fixed;
right: 0;
width: calc(100% - 200px);
}
在您的代码中添加此css
.right { position: sticky; top: 5px; }
不需要JavaScript。 使用position: fixed
为.right
div。
如果它消失了,给它一个宽度(可能大约80%)和right: 0
:
.right {
position: fixed;
width: 80%;
right: 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.