Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望能够使用 JS 为动态大小的图像的中心设置绝对 (x, y) 位置。必须有一些 CSS 容器的安排可以做到这一点,但我无法弄清楚。可以使用 JS 计算的偏移量,但它使动画缩放相当复杂,所以我想尽可能避免它。
我似乎找到了解决办法。
HTML:
<div id='parent'><div id='child'></div></div>
子 CSS:
transform: translate(-50%, -50%); -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%);
现在,在父级上设置翻译将使子级按其中心移动。