2013-05-13 224 views
0

我有一个链接位于链接到#here的页面顶部。我怎样才能使页面向下滚动到#here的锚链接?我一直在去Google和jQuery网站,但不知道该怎么做。 JS代码就是我所拥有的。使用jQuery滚动到锚点链接

HTML:

<div class="container"> 
    <div class="header"> 
     <a class="link" href="#here">Here</a> 
    </div> 

    <div class="footer"> 
     <a id="here"></a> 
     <p>Some text...</p> 
    </div> 
</div> 

JS:

$('.link').click(function() { 
    $(this).scroll() 
}); 

回答

2

您可以添加:

$('.link').click(function() { 
    $("html, body").animate({ scrollTop: $('#here').offset().top }, 1000); 
}); 
+0

谢谢。正是我在找什么。 – jfrosty 2013-05-13 23:58:13

+0

你并没有阻止这里的默认行为。 – Ohgodwhy 2013-05-14 00:07:50

0

如果您将浏览器的位置井号标签,然后在浏览器应该滚动到该位置。

$('.link').click(function() { 
    windows.location = $(this).attr('href'); 
}); 
+1

感谢您以及您的时间和帮助。 – jfrosty 2013-05-13 23:57:42

0

这个插件有一些不错的功能和特性加入到它(如不同的动画): http://demos.flesler.com/jquery/scrollTo/

“的所有匹配的元素将被滚动,例如:

$('div.pane').scrollTo(...);//all divs w/class pane

如果需要滚动窗口(屏幕),然后使用:

$.scrollTo(...);//the plugin will take care of this