简体   繁体   中英

How to scroll to a newly added dom element?

Say the user is viewing a particular part of the page, when behind the scenes an auto update feature runs, and right above or below the user's screen some more content is added.

I have the id of the newly added content's div.

How can I smoothly scroll the user to the newly added div using jquery or plain javascript? Must work in all major browsers.

这是我用于jQuery的内容

$('html,body').animate({scrollTop: jQuery("#ID").offset().top},'slow');

I think, this should work...with jquery.

$(window).scroll($('#newly-added-elem').offset().top);

UPDATE : smooth scrolling can be achieved with this:

$('body').animate({scrollTop: $('#newly-added-elem').offset().top},'slow');

Use an anchor:

<a name="newelement">.... new element stuff here ...</a>

then change the page's URL to point at that new element:

http://example.com/yourpage.html#newelement

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