2013-07-20 71 views
6

我使用下面的滾動到一個元素jQuery的滾動到元素的底部不頂

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

當滾動到它上面的代碼放置在瀏覽器窗口頂部的元素,是有辦法我可以滾動到滾動到瀏覽器窗口底部元素結束的元素?

回答

4

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

13

嘗試像這樣把滾動的元素

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

的底部或該放位置的元素。滾動的底部:

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

經過一些修改工作:'$( 「HTML,身體」),動畫({scrollTop的:$( '選擇')偏移()頂部 - ($(窗口)。高度() - $( 「選擇」)outerHe。 ight()+10)},500);' – ramo

相關問題