2014-07-03 28 views
0

我使用一些jQuery來做到以下幾點:jQuery的散列鏈接到新的頁面滾動到不同的錨

從第1頁的鏈接錨2頁 - 但首先加載頁面,然後滾動到錨。

<script type="text/javascript"> 
jQuery(function(){ 
    jQuery('html, body').animate({ 
     scrollTop: jQuery('.Anchor').offset().top 
    }, 2000); 
    return false; 
    }); 
</script> 

我的鏈接是這樣的domain.com/page2.html#anchor-name

的錨是這樣的:

<div name="anchor-name" class="Anchor">&nbsp;</div> 

以上工作正常,僅有1錨。

我該怎麼編輯上面的jQuery,這樣我就可以在第2頁有多個錨鏈接&鏈接。

<div name="anchor-name" class="Anchor">&nbsp;</div> 
<div name="anchor-fred" class="Anchor">&nbsp;</div> 
<div name="anchor-bert" class="Anchor">&nbsp;</div> 

回答

1

我認爲這應該工作?

鏈接到它作爲domain.com/page2.html#fred

<script type="text/javascript"> 
jQuery(function(){ 
    jQuery('html, body').animate({ 
     scrollTop: jQuery(window.location.hash + "-anchor").offset().top 
    }, 2000); 
    return false; 
    }); 
</script> 

<div id="name-anchor">&nbsp;</div> 
<div id="fred-anchor">&nbsp;</div> 
<div id="bert-anchor">&nbsp;</div> 
相關問題