CSS transform vs position

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

Can some please explain to me the difference in transitioning the positional left or right properties or the -transform: translateX(n), as the both seem to achieve the same thing yet can be applied independently. I understand about the possibility of hardware acceleration but that's dependent on implementation.

// psuedo code;  #box {     -transition-property: all;     -transition-duration: 1s; }  // javascript  box.style.transform = "translateX(200px)"; vs box.style.left = "200px"; 

What's the advantage of one over the other? Thanks,

p

回答1:

Position is dependant on what the position is set to, transform works from the element itself. So you could see transform as being identical to position:relative;.

However, you can still use transform on an absolutely positioned element (to position it relatively without needing an additional element or resorting to margins), as well as transform being CSS3 so if you can use position instead you should.



回答2:

top and left CSS properties work only on elements positioned relative, absolute or fixed. Also, top and left properties rely on the parent's position (relative it, absolute or static). Translations are not affected by that settings.


Translation transformations are "identical" to applying top and left when element has position: relative. In any other case they aren't the same operations.



回答3:

There is good article: Why Moving Elements With Translate() Is Better Than Pos:abs Top/left



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!