是否可以使用jQuery动画scrollTop?

14 浏览
0 Comments

是否可以使用jQuery动画scrollTop?

我想要平滑地滚动页面,我不想自己写一个函数实现,尤其是如果jQuery已经有了一个这样的函数。

admin 更改状态以发布 2023年5月23日
0
0 Comments

Nick的答案非常有效。在animate()调用中指定complete()函数时要小心,因为你声明了两个选择器(html和body),它会被执行两次。

$("html, body").animate(
    { scrollTop: "300px" },
    {
        complete : function(){
            alert('this alert will popup twice');
        }
    }
);

以下是避免双重回调的方法。

var completeCalled = false;
$("html, body").animate(
    { scrollTop: "300px" },
    {
        complete : function(){
            if(!completeCalled){
                completeCalled = true;
                alert('this alert will popup once');
            }
        }
    }
);

0
0 Comments

你可以使用 .animate() 方法来操作 scrollTop 属性,像这样:

$("html, body").animate({ scrollTop: "300px" });

0