简体   繁体   English

设置DOMElement边界的最快方法是什么?

[英]What's the fastest way to set DOMElement bounds?

Currently, to place an absolute element in DOM, I use : 当前,要在DOM中放置绝对元素,我使用:

this.myObject.style.left = aValue1 + 'px' ;
this.myObject.style.top = aValue2 + 'px' ;
this.myObject.style.width = aValue3 + 'px' ;
this.myObject.style.height = aValue4 + 'px' ;

Is there a better (quick for browsers) way to do this ? 有没有更好的方法(快速浏览器)来做到这一点?

Also, maybe do I remove the element from DOM and re-append after ? 另外,也许我应该从DOM中删除该元素并在之后重新添加?

Thanks for your anwsers. 谢谢你的回答。

There is a topic about this here Create Document Fragment . 在此处创建文档片段有关此主题。

In my opinion what you're doing is fine, but if you're doing it a lot then you might consider taking the node out of the main html then putting it back in. Especially if you are doing a lot of changes on one node. 以我的观点,您做的很好,但是如果您做了很多,则可以考虑将节点从主要html中删除,然后放回原处。尤其是如果您在一个节点上进行了很多更改。

Replacing the node is faster because the browsers reflow the page for every dom manipulation. 替换节点的速度更快,因为浏览器会在每次dom操作时重排页面。 Each change on the node while it's still in the dom is that many reflows. 节点仍在dom中时,节点上的每个更改都是许多重排。 A bunch of manipulations and then inserting the changed node is only one reflow. 一堆操作,然后插入更改的节点,只是一个重排。

Edit: An good article about this: The new game show: “Will it reflow?” 编辑:关于此的一篇好文章: 新游戏节目:“它会回流吗?”

Cloning the specific nodes would probably be the fastest if there are a lot to update. 如果有很多要更新,则克隆特定节点可能是最快的。 If it's just one or a couple nodes the update may not benefit from cloning or removal and replace. 如果只是一个或几个节点,则更新可能无法从克隆或删除和替换中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM