Jquery 平滑滚动到同一个顶部锚点
我有一个问题,我的smooth scroll jquery每次都指向同一个锚点……下面是我的代码:Jquery 平滑滚动到同一个顶部锚点,jquery,html,Jquery,Html,我有一个问题,我的smooth scroll jquery每次都指向同一个锚点……下面是我的代码: <a href="#ax">a</a> <a href="#b">a</a> Takes you too... <a name="ax" id="ax"></a> <a name="b" id="b"></a> $('a[href*=#]').click(function(){ $('html, bo
<a href="#ax">a</a>
<a href="#b">a</a>
Takes you too...
<a name="ax" id="ax"></a>
<a name="b" id="b"></a>
$('a[href*=#]').click(function(){
$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
return false;
});
它会把你带到字母表中的每一个字母,我把它命名为ax,以防a与等混淆。所以问题是它总是指向a
我觉得jquery不是问题所在,我只是缺少了一个概念
非常感谢使用此更优化的功能进行平滑滚动
$(function() {
$('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;
}
}
});
});
你能为它创建jsfiddlr吗?该死的,这不起作用,现在完全停止了平滑的动画:你能为你的代码创建jsfiddle吗?我将尝试修复。