2013-05-09 76 views
12

當用戶關閉模式(特別是基金會4中的Reveal模式)時,我想滾動到頁面的最右邊緣,這個頁面非常寬(故意寬)。如何在水平溢出的頁面上向右滾動?

我已經嘗試在div上對齊一直正確,但顯然不起作用的id。

+0

好吧,這是在互聯網上的其他地方回答得比我能更準確地回答它:[http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizo​​ntal-page-scrolling-with- jquery /](http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizo​​ntal-page-scrolling-with-jquery/) – 2013-05-09 15:33:57

回答

27

要水平滾動,請使用scrollLeft()

$('body, html').scrollLeft(400); 

jQuery還支持動畫scrollLeft屬性。

一路向右滾動,你得到的頁面的整個寬度,並減去窗口寬度以獲得左邊緣:

var left = $(document).outerWidth() - $(window).width(); 
$('body, html').scrollLeft(left); 
+0

太棒了!謝謝!我結束了基於你的幫助:$('body,hmtl')。animate({scrollLeft:'+ = 500'},1000);' – 2013-05-09 15:45:41

+0

我用這個解決方案來解決類似的問題, ('selector')[0] .scrollWidth - $('selector')。width()'將元素的'scrollWidth'屬性與'width – mopo922 2017-08-12 02:24:02

3

實際上,你可以用一個div ID做到這一點。如果你有這樣一個div:

<div id="divid" style="position: absolute; left: 9000px;">Far Right</div> 

然後,只需改變你的location.hash匹配:

location.hash = "divid"; 

瀏覽器將採取滾動你的照顧。

0

好了,所以這裏是一個工作示例:

$(document).ready(function(){ 
    $('body').scrollLeft($(document).outerWidth()); 
}); 

你甚至可以設置scrollLeft到一個更高:http://jsfiddle.net/B2aQF/1/

基本上,你可以使用jQuery的scrollLeft功能設置的身體左側滾動位置數量比你所需要的還要多,因爲它不會過度滾動。