简体   繁体   中英

Does jQuery have any functions to scroll the client to the bottom of the view port?

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

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

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).

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

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

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);

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM