簡體   English   中英

jQuery滾動到元素的底部而不是頂部

[英]jQuery scroll to bottom of element not top

我使用以下內容滾動到一個元素

$("html, body").animate({
  scrollTop: $('selector').offset().top
}, 500);

當滾動到它時,上面的代碼將元素放在瀏覽器窗口的頂部,是否有一種方法可以滾動到元素,滾動結尾與瀏覽器窗口底部的元素?

嘗試這樣的東西將滾動放在元素的底部

$("html, body").animate({
      scrollTop: $('selector').offset().top + $('selector').outerHeight(true)
    }, 500);

或者將元素放在滾動的底部:

$("html, body").animate({
          scrollTop: $('selector').offset().top + $('selector').outerHeight(true) -$(window).height()
        }, 500);

您可以使用窗口的高度來計算滾動位置

暫無
暫無

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

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