2011-02-15 100 views
0

我有一個簡單的jQuery點擊和scrollTO錨腳本。我有另一個頁面,其中我鏈接的內容爲index.php#home,index.php#約等...從另一頁面頁面動畫

如何從外部頁面實現scrollTo效果?我正在考慮將該部分鏈接爲index.php?page = home,並且在頁面加載時,取回home變量並應用動畫。

還有其他想法嗎?

編輯,我的代碼如下

$("nav a").click(function(event){ 
//prevent the default action for the click event 
//event.preventDefault(); 

//get the full url - like mysitecom/index.htm#home 
var full_url = this.href; 

//split the url by # and get the anchor target name - home in mysitecom/index.htm#home 
var parts = full_url.split("#"); 
var trgt = parts[1]; 

//get the top offset of the target anchor 
var target_offset = $("#"+trgt).offset(); 
var target_top = target_offset.top; 

//goto that anchor by setting the body scroll top to anchor top 
$('html, body').animate({scrollTop:target_top}, 500); 
}); 

回答

1

我想的index.php?頁=家裏是一個簡單的方法去。

雖然在PHP測試產生了使用

if (!empty($_REQUEST['page'])) 

確保你使用它

$sanitizedPage = htmlentities($_REQUEST['page']); 

輸出之前過濾所有的用戶輸入的變量被設置的index.php的JavaScript到您的網頁當頁面加載時調用適當的scrollto動作

<script type="text/javascript"> 
    window.onload = functionThatScrolls('<?php echo $sanitizedPage; ?>'); 
</script> 

這裏我認爲你有一個javascript函數,它將錨點名稱作爲參數來引起scrollTo效果。如果你正在使用jquery或其他框架,當然使用它們的onload事件處理程序。