[英]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.