jQuery: move window viewport to show freshly toggled element

前提是你 提交于 2019-12-02 20:52:38

Check out the scrollTo jQuery plugin. You can simply do something like this:

$.scrollTo('div#addnote-area');

Or, if you want to animate it, provide the # of milliseconds for the scrolling to last:

$.scrollTo('div#addnote-area', 500);

Without the scrollTo plugin

$(window).scrollTop($('div#addnote-area').offset().top)

EDIT: Well I sure get a lot of points from this old answer :)

Here's a bonus, this can also be animated.

Just use the animate() function and target the body tag:

$('body').animate({scrollTop:$('div#addnote-area').offset().top},500)

Try it on Stackoverflow! Open the inspector console and run

$('body').animate({scrollTop:$('#footer').offset().top},500)

jQuery's scrollTop also works. Try setting like:

 $('#idOfElement').css('scrollTop', 10)

You can get height/width pretty easily using $(...).offset().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!