CSS transform通过rotate()和scale()实现元素旋转缩放,配合transform-origin设置变换基点,不影响文档流且可GPU加速;组合函数按书写顺序执行,transition可实现平滑动画,需注意性能优化与浏览器兼容性。

CSS的
transform
rotate()
scale()
要通过CSS
transform
transform
rotate()
scale()
transform
1. 旋转 (Rotate)
立即学习“前端免费学习笔记(深入)”;
rotate()
transform-origin
rotate(angle)
angle
deg
rad
grad
turn
rotate(45deg)
rotate(-90deg)
rotate(0.5turn)
示例代码:
.rotate-element {
    transform: rotate(30deg); /* 顺时针旋转30度 */
}
.rotate-on-hover:hover {
    transform: rotate(180deg); /* 鼠标悬停时旋转180度 */
    transition: transform 0.5s ease-in-out; /* 平滑过渡 */
}2. 缩放 (Scale)
scale()
scale(factor)
factor
scale(sx, sy)
sx
sy
factor
scale(1.5)
scale(0.8)
scale(2, 0.5)
示例代码:
.scale-element {
    transform: scale(1.2); /* 放大1.2倍 */
}
.scale-on-hover:hover {
    transform: scale(1.1, 1.3); /* 鼠标悬停时宽度放大1.1倍,高度放大1.3倍 */
    transition: transform 0.3s ease; /* 平滑过渡 */
}3. 组合变换
可以将
rotate()
scale()
transform
示例代码:
.combined-transform {
    transform: rotate(45deg) scale(1.2); /* 先旋转45度,再放大1.2倍 */
}
.combined-on-hover:hover {
    transform: scale(1.1) rotate(15deg); /* 鼠标悬停时,先放大1.1倍,再旋转15度 */
    transition: transform 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55); /* 自定义过渡效果 */
}4. transform-origin
transform-origin
50% 50%
transform-origin: x-position y-position z-position;
x-position
y-position
left
center
right
top
bottom
px
em
transform-origin: top left;
.custom-origin-rotate {
    transform-origin: 0 0; /* 变换基点设为左上角 */
    transform: rotate(30deg);
}
.custom-origin-scale {
    transform-origin: bottom right; /* 变换基点设为右下角 */
    transform: scale(1.1);
}transform
transform-origin
transform
width
height
margin
padding
transform
transform
至于
transform-origin
50% 50%
transform-origin
top left
bottom right
25% 75%
10px 20px
transform-origin
left center
0 50%
transform-origin
transform-origin
transform
transition
组合多个
transform
transform
rotate(A) scale(B)
scale(B) rotate(A)
rotate(45deg)
scale(1.5)
scale(1.5)
rotate(45deg)
transform-origin
至于平滑动画,
transition
transform
transition
.animated-box {
    width: 100px;
    height: 100px;
    background-color: dodgerblue;
    transition: transform 0.4s ease-out; /* 关键:定义transform属性的过渡 */
}
.animated-box:hover {
    transform: translateX(50px) rotate(90deg) scale(1.2); /* 鼠标悬停时触发变换 */
}这里,
transition: transform 0.4s ease-out;
transform
ease-out
transform
transition
opacity
color
@keyframes
animation
transition
transform
在使用
transform
性能考量:
transform
transform
transform
top
left
width
height
will-change
will-change: transform;
transform
will-change
transform
scale()
潜在的兼容性问题:
transform
transform
–ms-
transform
-webkit-transform
-moz-transform
-o-transform
-ms-transform
transform
transform
transform
transform
z-index
transform
inline
transform
inline
<span>
transform
inline-block
block
总的来说,
transform
以上就是如何通过csstransform实现元素旋转缩放的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号