如何使用jQuery在点击时向下滚动特定距离

3
我想在点击一个带有动画的按钮时将页面向下滚动到一定距离,目前我可以滚动到某个元素,但这不是我想要的,我想滚动到一个指定距离(以像素为单位)。
我已经尝试了很多变化,但它们似乎在我的情况下不起作用,至少不是我想要的方式。
无论如何,以下是代码:
$(document).ready(function(){
   $('.next_section').click(function(){
        $('html, body').animate({
            scrollTop: $('.img_div').offset().top
        }, 1000);
   }); 
}
1个回答

11

到一定距离

我假设这里的距离是以像素为单位的?

这会向下滚动150个像素:

var y = $(window).scrollTop();  //your current y position on the page
$(window).scrollTop(y+150);

@therealbischero(谢谢!)在评论中提到了我的回答缺少的部分,即如何使其平稳滚动:

var y = $(window).scrollTop();
 $('html, body').animate({ scrollTop: y + 150 }) 

这很棒,有什么办法可以使它平滑滚动,我猜是用animate()或类似的东西对吧? - vuffer
2
将第二行替换为 $('html, body').animate({ scrollTop: y + 150 }) - therealbischero
非常感谢大家,你们是最棒的。 - vuffer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接