2014-12-13 46 views
0

我在我的頭文件中有這個代碼,它將所有頁面滾動到特定的錨點。在jQuery中添加異常

我想爲我的主頁添加一個例外,所以代碼將無法在那裏工作。

// this code is between <head> tags in my header file. 
<script 
function scroll(){ 
$('html, body').animate({ 
    scrollTop: $('#scroll').offset().top 
    }, 1000); 

} 
</script> 

// this code is at the bottom of the page in my header file 
// anchor 
<a href="#scroll" id="scroll"></a> 

// this code is in the header file 
// called 
<body onload="scroll()"> 
+0

你想要添加異常是什麼意思,爲什麼你想讓你的代碼失敗? – Panther 2014-12-13 17:06:55

+0

這樣做的一種方法是檢查你的URL是否在你的主頁上,如果是 - 禁用滾動。 – Vucko 2014-12-13 17:07:31

+0

您使用什麼服務器端語言來創建HTML?有什麼表明該腳本目前正在主頁上運行 – twinlakes 2014-12-13 17:07:59

回答

0

您可以覆蓋滾動()函數

function scroll(){ 
// this function do nothing 

} 

,並把它放在一個合適的地方,如在頁面的頂部或頁面的相關標籤到底

例如

<html> 
<head> 
<script> 
function scroll() 
{ 
alert(1); 
} 
</script> 
</head> 

<body onload="scroll()"> 
body text 

<script> 
function scroll() 
{ 
alert(2); 
} 
</script> 

</body> 
</html> 

運行此代碼alert(2)

+0

我正在使用wordpress。我怎樣才能找到主頁來放置代碼? – 2014-12-13 17:38:08

+0

請參閱更新。把你的第二個覆蓋在之前或者在你的wordpress中做一些嘗試 – 2014-12-13 17:39:21

+0

如果我這樣做,函數滾動不再起作用。我只想在主頁上設置例外。在我的主頁上,我不想讓該腳本運行,但在其他人上我希望代碼處於活動狀態。 – 2014-12-13 17:42:53