2011-09-21 30 views
0

想通這需要一個更好的解釋:Howto:頁面加載後的Jquery Scrollto動作點擊?

我有一些鏈接導航:

<a id="home" href="/">Home</a> 
<a id="screenshots" class="scroll_to_screenshots" href="#">Screenshots</a> 
<a id="ask" class="scroll_to_container" href="/ask">Support</a> 

而且我有以下內容的js文件:

$(document).ready(function() { 
$(".scroll_to_screenshots").click(function() {$.scrollTo($("#screenshots").position().top+10, 300)}); 
$(".scroll_to_container").click(function() {$.scrollTo($("#container").position().top-10, 300)}); 
}); 

現在的截圖它工作正常:它滾動到屏幕截圖ID。然而,第二個需要先加載一個頁面,然後滾動。顯然,這是行不通的(它簡單地向下滾動,然後頁面加載踢。我可以改變該行動 - 首先加載頁面,向下滾動?

然後,當然我有另一個問題,當我在。 ../ask第一個鏈接不工作了

回答

0

簡單

你應該在頁面加載功能使用:。

ScriptManager.RegisterStartupScript(this, GetType(), ToString(), "$.scrollTo($('#container').position().top-40, 300)", true); 
+0

感謝您的回答。我不是那種先進的jQuery,它是如何轉化爲我的上面的代碼? – Simon

+0

我認爲上面的答案是針對asp.net ajax的。 – Jags

+0

???也爲純淨的asp.net頁面 - 每個頁面加載他應該添加此func –

0

把這個接觸頁面

<script type="text/javascript"> 
    $(function(){ 
    $.scrollTo($("#container").position().top-40, 300); 
    }); 
</script> 
+0

謝謝詹姆斯。有沒有辦法在js文件中定義它?喜歡:先打開頁面,然後滾動到。 – Simon

+0

yes..just在你內部添加$(function()...)部分聯繫js – Jags

+0

我在原始文章中添加了我的更改,您可能會發現我在那裏做了錯誤。 – Simon

0

所以這是訣竅。但是,如果有人有一個更精緻,優雅的答案,我耳熟能詳。

// on the /example slug only 
var fragment = 'example'; 

var url = window.location.href.split('/'); 

var last_item = url[url.length-2]; 
last_item = last_item.replace(/(\?|\#).*/, ''); 

if (last_item == fragment) { 
    setTimeout(function() {$.scrollTo($("ul#posts").position().top, 300);}, 1000); 
0

而不是使用這個的:

$(document).ready(function(){ 
    // Your stuff here 
}); 

嘗試使用這樣的:

$(window).load(function(){ 
    // Your stuff here 
}); 

這將使得腳本等待網頁上的所有元素在它滾動之前加載。