簡體   English   中英

使 Div 固定在滾動上

[英]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";
}

我遇到過這篇文章,其中介紹了一種解決方案https://www.virendrachandak.com/techtalk/make-a-div-stick-to-top-when-scrolled-to/

不知道這是什么意思嗎?

但是您可以添加CSS屬性位置:fixed; 使其即使在滾動后仍顯示在sam位置上。

有關CSS定位的更多信息

它接縫你正在尋找的position: sticky; .

了解更多: https : //css-tricks.com/position-sticky-2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM