使用 jQuery,如何将 div 滚动 N 个像素?
到目前为止我已经尝试过了
$("div.foo").scrollTop(75); //scroll down by 75 pixels
最佳答案
来自scrollTop 的 jQuery 文档:
Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.
您调用的函数版本设置滚动条的垂直位置从元素顶部开始,而不是从当前位置开始。因此,为了从当前位置向下滚动,您首先必须检索当前位置。有几种方法可以做到这一点,但你可以这样做:
var $foo = $("div.foo");
$foo.scrollTop($foo.scrollTop() + 75); // scroll 75px down from current
关于javascript - jquery:如何将div滚动n个像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43922640/