[英]Prevent page from scrolling to top
我對jQuery Mobile和錨鏈接的URL有一點問題。
當頁面被加載時,jquery throw事件可以正常工作,但是jquery執行代碼並將頁面移到頁面頂部。 *我的問題不是在同一頁面中鏈接錨,而是在鏈接中使用URL鏈接另一個頁面,例如: example_jquery.html#wopwop
我寫了一個小例子,看看看不見(您可以在任何瀏覽器中進行測試):
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css">
</head>
<body>
<div data-role="page">
<h1>wopwop</h1>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
<a id="wopwop"></a>
<h1>wopwop</h1>
</div>
</body>
我寫了一個補丁看棧中的帖子:
<script type="text/javascript">
setTimeout(function(){
if(location.hash == "#wopwop"){
$.mobile.silentScroll($('#wopwop').get(0).offsetTop);
}
}, 700);
</script>
但是我不認為這是解決方案,您知道如何進行這項工作嗎?
謝謝。 對不起,我英語不好
顯示頁面時jQuery Mobile的默認行為是在顯示頁面時滾動到頂部。 scroll的值始終為0
並存儲在$.mobile.defaultHomeScroll
。
您需要在任何頁面事件上但在完全顯示頁面並完成轉換之前覆蓋此值。
if (location.hash == "#wopwop") {
$.mobile.defaultHomeScroll = $('#wopwop').offset().top;
}
這將強制滾動到wopwop
div在頁面中的偏移量,而無需滾動到頂部然后再返回到該div。
$(document).ready(function(){
$('html, body').animate({ scrollTop: $("#wopwop").offset().top }, 7000);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.