我使用JavaScript导航到主页上的锚。从主页中,当我点击一个锚链接,网址是一样的:本地主机/(使用JavaScript ScrollTo),但是当我尝试从一个单独的页面链接到的锚标记它显示在URL中的锚标记。本地主机/#锚。Rails的隐藏#anchor标签在URL
我怎么能掩盖的URL不显示锚标记?
<li><%= link_to "Anchor", root_path(:anchor => 'anchor') %></li>
的jQuery:
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
哪个库提供了这个scrollTo方法,prototype? jQuery的?答案可能完全不同。 – 2013-04-26 05:09:36
是的,我使用jQuery。代码粘贴在下面。 – agassi0430 2013-04-27 06:06:40