这里有一些代码,我写了 -
$('#go').on('click', function(e){
e.preventDefault()
var href = $('#go').attr('href');
console.log(href)
$('html, body').animate({
scrollTop: $(href).offset().top
}, )
})
我不确定为什么会这样,谢谢你的帮助!!!
答案 0 :(得分:0)
在animate
函数中添加一些时间值,例如animate((),1000)
。它们会产生平滑效果。请参阅 jquery documentation of animate()
$('#go').on('click', function(e){
e.preventDefault()
var href = $('#go').attr('href');
console.log(href)
$('html, body').animate({
scrollTop: $(href).offset().top
},1000 )
})
答案 1 :(得分:0)
您需要查看的两件事
#
<a id="go" href="#goTo">GOOO</a>
$('#go').on('click', function(e){
e.preventDefault()
var href = $('#go').attr('href');
$('html, body').animate({
scrollTop: $(href).offset().top
}, 1000)
})
div {
margin-top:100vh;
height:100px;
background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="go" href="#goTo">GOOO</a>
<div id="goTo">
</div>
答案 2 :(得分:0)
好吧,如果您使用的是像Material Design lite这样的CSS框架,那么您需要使用不同的东西。
adjust_text
将其包装成“.mdl-layout__content”,而不是“html,body”,因为它修复了它。我有同样的问题,但修复了它:)