Javascript 单击链接滚动到元素,然后向元素添加一个类
我有以下html标记:Javascript 单击链接滚动到元素,然后向元素添加一个类,javascript,jquery,html,Javascript,Jquery,Html,我有以下html标记: <div id="main"> <p>some text</p> <a id="goto-notes" href="#">see notes</a> <p>some text...</p> <div id="notes"> <p>notes here</p> </div> </di
<div id="main">
<p>some text</p>
<a id="goto-notes" href="#">see notes</a>
<p>some text...</p>
<div id="notes">
<p>notes here</p>
</div>
</div>
目前div#notes位于链接下方的500px+上
#notes
的顶部位置,如下所示:$('#notes').position().top
animate()
有一个complete:function()
在动画完成后调用$('#goto-notes').click(function(){
var n = $('#notes);
$('#main').animate({
scrollTop: n.position().top
},{
duration: 2000,
complete: function(){
n.addClass('seen');
}
});
});
这里是
#notes
的顶部位置,如下所示:$('#notes').position().top
animate()
有一个complete:function()
在动画完成后调用$('#goto-notes').click(function(){
var n = $('#notes);
$('#main').animate({
scrollTop: n.position().top
},{
duration: 2000,
complete: function(){
n.addClass('seen');
}
});
});
这是你能用你的css添加到吗?这个答案不需要css…是的,对不起,自从OP提到css转换后,我认为需要css。你能用css添加到吗?这个答案不需要css…是的,对不起,自从OP提到css转换后,我认为需要css。为什么要动画$('body')?对不起,我解释得不好。我体内有很多东西。此内容仅在我的div#main上。当我点击你的小提琴时,它不会向下滚动为什么要设置$('body')?对不起,我解释得不好。我体内有很多东西。此内容仅在我的div#main上。当我点击你的小提琴时,它不会向下滚动谢谢@Mike Barwick!谢谢@Mike Barwick!
$('#goto-notes').click(function(){
var n = $('#notes);
$('#main').animate({
scrollTop: n.position().top
},{
duration: 2000,
complete: function(){
n.addClass('seen');
}
});
});