admin管理员组文章数量:1429444
Is there a way to encapsulate several DOM manipulating mands in a transaction so that the content would not "flicker about"? Something like this:
window.stopDrawing(); // start transaction
$("#news").append("<div>a new news item</div>");
// ... do something more
$("#news").css("top", "-150px");
window.startDrawing(); // stop transaction
Is there a way to encapsulate several DOM manipulating mands in a transaction so that the content would not "flicker about"? Something like this:
window.stopDrawing(); // start transaction
$("#news").append("<div>a new news item</div>");
// ... do something more
$("#news").css("top", "-150px");
window.startDrawing(); // stop transaction
Share
Improve this question
asked Aug 28, 2011 at 12:45
duality_duality_
18.8k24 gold badges84 silver badges96 bronze badges
1
- 1 Browsers (newer ones) do this already, internally, to the extent that they can. There's no explicit control however. If you post more about a particular situation you're seeing, somebody may be able to suggest an optimization. – Pointy Commented Aug 28, 2011 at 12:51
2 Answers
Reset to default 5Everytime you must update a large set of elements just set up a function including all the operations, call mozRequestAnimationFrame(or webkitRequestAnimationFrame), only after your function is finished executing pletely it will draw your changes to the screen.
More at: https://developer.mozilla/en/DOM/window.mozRequestAnimationFrame
I believe browsers are redrawing the document only when the script returns to the even loop, so it won't renders anything until the end of your script.
Anyway some operations may force the browser to do some costly operations such as reputing the dimensions of elements on the document (e.g. when retrieving the dimensions of an element after some DOM manipulations have been made). So you should do DOM manipulations off-document as much as possible.
If you have many manipulations to do on some element, it may be cheaper to remove it from the document tree, do your manipulations, and add it to the tree again.
本文标签: HTML DOM Drawing Transaction in JavascriptStack Overflow
版权声明:本文标题:HTML DOM Drawing Transaction in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745495046a2660770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论