2015-01-21 38 views
0

平滑滾動對同一頁面正常工作。但是,我希望平滑滾動以在點擊時用於其他頁面。例如, 當我點擊主頁上的服務鏈接時,我需要轉到該服務頁面並平滑地滾動到該div標籤。你能幫我解決這個問題嗎?jQuery平滑滾動到新頁面鏈接

HTML代碼

 <ul class="secondmenu"> 
      <li><a href="Home.html#main1">Home</a></li> 
      <li><a href="about.html#main2">About</a></li> 
      <li><a href="services.html#templatemo_main">Technology</a></li> 
      <li><a href="about.html#templatemo_main">Team</a></li> 
     </ul> 

jQuery的

jQuery('.secondmenu a').click(function (e) { 
e.preventDefault(); 
var target = this.hash; 
if (this.href != '#') { 
    jQuery('html, body').animate({ 
     scrollTop: jQuery(target).offset().top - 60 
    }, 1000); 
} }); 

回答

1

既然你必須去相應的節溫頁面加載後,你應該ready函數內編寫腳本。

<script> 
     $(document).ready(function(e) { 
     var target = window.location.hash; //The hash property sets or returns the anchor part of a URL, including the hash sign 
     if (typeof target != "undefined") { //Checks if the variable is undefined incase no hash property exists 
      $('html, body').animate({ 
       scrollTop: $(target).offset().top - 60 
      }, 1000); 
      e.preventDefault(); 
     } 
    }); 
</script> 

希望幫助ü交配.. :)

+0

我不是jquey專家,我沒有得到你的想法:( – user3363577 2015-01-21 05:18:50

+0

ü不必成爲專家,瞭解這個mate..use這個代碼正確後jquery被加載 – Outlooker 2015-01-21 05:24:22

+0

好吧,我添加了它,現在當我點擊我的主頁鏈接什麼都沒有發生。 我的上面的jquery代碼有什麼不對嗎? – user3363577 2015-01-21 05:29:15