我一直在尝试让滚动到div id的jquery代码能够正常工作。基于另一个堆栈溢出问题,我尝试了以下方法
演示http://jsfiddle.net/kevinPHPkevin/8tLdq/
$('#myButton').click(function() {
$.scrollTo($('#myDiv'), 1000);
});
但它并没有起作用。它只是捕捉到div。我也试过了
$('#myButton').click(function(event) {
event.preventDefault();
$.scrollTo($('#myDiv'), 1000);
});
没有任何进展。
发布于 2013-09-26 02:36:46
您需要为html, body
设置动画
演示http://jsfiddle.net/kevinPHPkevin/8tLdq/1/
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#myDiv").offset().top
}, 2000);
});
发布于 2014-03-17 21:16:24
这是我的2分钱:
Javascript:
$('.scroll').click(function() {
$('body').animate({
scrollTop: eval($('#' + $(this).attr('target')).offset().top - 70)
}, 1000);
});
Html:
<a class="scroll" target="contact">Contact</a>
目标是:
<h2 id="contact">Contact</h2>
发布于 2020-05-21 00:34:02
普通JS:
如果你使用现代浏览器,可以用普通的JS来完成。
document
.getElementById("range-calculator")
.scrollIntoView({ behavior: "smooth" });
浏览器支持是一个小问题,但是modern browsers support it。
https://stackoverflow.com/questions/19012495
复制相似问题