CSS:如何从中心而不是左上角缩放图像

Eol*_*lis 17 css transition center scale

所以我的问题是我有一个图像,我设置它的CSS有一个

max-width: 100% 
Run Code Online (Sandbox Code Playgroud)

它以较低的分辨率进行缩放(如下面的小提琴所示).我想要的是过渡从图像中心生效.

目前; 从我从大多数情况看到的我已经完成的涉及比例的过渡,他们从左上角扩展.

这是我的小提琴:http://jsfiddle.net/Eolis/3ya98xh8/3/

Gil*_*mbo 29

只需替换width: 400px;transform: scale(2,2)on :hover.

img {
    width: 100%; max-width: 100%;
}
div {
    position: absolute; left: 20%; top: 20%;
    width: 250px;
    transition: all 2s ease-in-out;
}
div:hover {
    transform: scale(2,2)
}
Run Code Online (Sandbox Code Playgroud)
<div>
    <a href="http://photobucket.com/images/cat" target="_blank">
        <img src="http://i583.photobucket.com/albums/ss278/campipr/coolcat.gif" border="0" alt="cat photo: cat coolcat.gif"/>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

  • @oCcSking是默认值`默认值:50%50%0`如规范https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin`初始值50%50% 0`。 (2认同)

Anu*_*sak 7

将此添加到div:hover:

transform: translateX(-25%) translateY(-25%);
Run Code Online (Sandbox Code Playgroud)

这是小提琴:http://jsfiddle.net/3ya98xh8/4/

  • 这是一个更好的方法:http://jsfiddle.net/3ya98xh8/6/.在这里,您不必使用任何硬编码值. (3认同)

小智 6

不要忘记设置变换原点

.current{
    top: 0px;
    left: 0px;
    z-index: 2;
    opacity: 1;
    transform: scale(0.9, 0.9);
    transition: opacity 1.6s ease-out 0s, transform 7.2s linear 0s;
    transform-origin: center center;
    animation: normal;
}
Run Code Online (Sandbox Code Playgroud)

https://css-tricks.com/almanac/properties/t/transform-origin/