[英]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. 我有新添加内容的div的id。
How can I smoothly scroll the user to the newly added div using jquery or plain javascript? 如何使用jquery或普通javascript顺利滚动用户到新添加的div? Must work in all major browsers. 必须适用于所有主流浏览器。
这是我用于jQuery的内容
$('html,body').animate({scrollTop: jQuery("#ID").offset().top},'slow');
I think, this should work...with jquery. 我认为,这应该有效...用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: 然后将页面的URL更改为指向该新元素:
http://example.com/yourpage.html#newelement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.