最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll to a newly added dom element? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jun 6, 2012 at 14:03 AliAli 268k269 gold badges592 silver badges786 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Here's what I use for 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./yourpage.html#newelement
发布评论

评论列表(0)

  1. 暂无评论