jQuery 是否有任何函数可以将客户端滚动到视口底部?

发布于 2024-07-15 23:50:56 字数 106 浏览 5 评论 0原文

我想用 jQuery 将动画滚动到视口底部。 是否有一个可用的插件并不过分(即对于这个小任务没有大量的功能集);

是否有可用的插件或使用 jQuery 本地执行此操作的方法?

I want to animate a scroll to the bottom of the viewport with jQuery. Is there a plugin available which isn't overkill (i.e. without a huge feature set for this small task);

Is there a plugin available or a way to do this natively with jQuery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰雪梦之恋 2024-07-22 23:50:56

jQuery 使这样的事情变得如此简单,以至于您不需要插件。 示例:

var x = 250; //insert your own formula to calculate where you want to scroll to in px
var t = 500; //arbitrary time in ms
$("html,body").animate({ scrollTop: x }, t);

您可以放置​​任何滚动的元素,例如 div,而不是 html,bodyt 是动画运行的时间(以毫秒为单位),x 是滚动到的位置(以 px 为单位)。 请注意,这也适用于scrollLeft,但不适用于scrollRight 或scrollBottom(不是jQuery 的限制,而是JavaScript 的限制)。

jQuery makes things like this so trivial that you just dont need a plugin. Example:

var x = 250; //insert your own formula to calculate where you want to scroll to in px
var t = 500; //arbitrary time in ms
$("html,body").animate({ scrollTop: x }, t);

Instead of html,body you can put any element which scrolls, like a div. t is the time in ms over which the animation will run and x is your position to scroll to in px. Note that this works with scrollLeft also but not scrollRight or scrollBottom (not a limitation of jQuery but JavaScript).

清晨说晚安 2024-07-22 23:50:56

您始终可以执行以下操作将元素滚动到底部

$("body").attr({ scrollTop: $("body").attr("scrollHeight") });

you can always do the following line to scroll an element to the bottom

$("body").attr({ scrollTop: $("body").attr("scrollHeight") });
季末如歌 2024-07-22 23:50:56

检查 jQuery.ScrollTo 插件,您可以滚动到确定的位置(固定或绝对),使用选择器、DOM 元素等等...

查看演示...

Check the jQuery.ScrollTo plugin, you can scroll to determined positions (fixed or absolute), using selectors, DOM elements, and more...

Give a look to the demos...

堇色安年 2024-07-22 23:50:56

为了详细说明 Darko Z 和 CMS 的答案,以下是我用来动画滚动到特定元素的内容:

var target = $('#elem');
$('html,body').animate({scrollTop: target.offset().top}, 500);

To elaborate on the answers from Darko Z and CMS, here is what I used to animate scrolling to a specific element:

var target = $('#elem');
$('html,body').animate({scrollTop: target.offset().top}, 500);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文