2015-01-21 74 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