How to set the transform origin to a specific point on the element?

后端 未结 3 1999
萌比男神i
萌比男神i 2020-12-20 17:40

In this example I want to rotate the hammer from its bottom so is there a way to know exactly the right coordinates of a specific point on the element or should I randomly t

3条回答
  •  生来不讨喜
    2020-12-20 17:59

    To make the transform-origin point relative to the element, you need to use transform-box: fill-box;.

    Chrome doesn't support that property yet (CSS transform-box - Chrome Platform Status), but luckily (yet wrongfully) Chrome sets the transform-origin relative to the element if you use percentages instead of pixels (https://css-tricks.com/transforms-on-svg-elements/).

    So, to make something that works on most *) modern browsers, use transform-box: fill-box; and transform-origin: xx% yy%;

    .hammer-icon {
        transform-origin: 15% 80%;
        transform-box: fill-box;
        ...
    }
    

    https://jsfiddle.net/L1790vzo/8

    *) IE/Edge doesn't support CSS transforms on SVG elements at all.
    *) Proper support in Chrome v64 and Opera v51

提交回复
热议问题