[英]Making a Div fixed on Scrolling
如何使 div 固定在用戶檢測滾動時。
示例:Facebook 的右側邊欄,到達某個滾動位置時它會卡住。
position:fixed
就是答案。
但是,如果您想知道網站的工作方式,則可以隨時查看網站的來源。 很有教育意義!
監視我們是否正在滾動。
if($(window).scrollTop() > 0){
//we're scrolling our position is greater than 0 from the top of the page.
$("#element").css({'position' : 'fixed'});
}
* 編輯
不用jQuery就可以做到。
if(window.scrollTop() > 0){
document.getElementById('element').style.position="fixed";
}
它接縫你正在尋找的position: sticky;
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.