JavaScript 图片360旋转动画是通过JavaScript控制图片元素在页面上进行连续旋转的视觉效果。这种效果通常用于展示产品、增强用户体验或作为页面装饰。
transform
属性和animation
属性实现。requestAnimationFrame
)控制图片旋转。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image 360 Rotate</title>
<style>
.rotate-image {
width: 200px;
height: 200px;
animation: rotation 5s infinite linear;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
</style>
</head>
<body>
<img src="your-image.jpg" alt="Rotating Image" class="rotate-image">
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image 360 Rotate</title>
<style>
.rotate-image {
width: 200px;
height: 200px;
}
</style>
</head>
<body>
<img id="rotatingImage" src="your-image.jpg" alt="Rotating Image" class="rotate-image">
<script>
const image = document.getElementById('rotatingImage');
let angle = 0;
function rotate() {
angle += 1;
image.style.transform = `rotate(${angle}deg)`;
requestAnimationFrame(rotate);
}
rotate();
</script>
</body>
</html>
问题:动画运行不流畅或有卡顿现象。 原因:
解决方法:
transform: translateZ(0);
启用GPU加速。requestAnimationFrame
代替setInterval
或setTimeout
。通过上述方法可以有效提升动画的流畅性和性能。
领取专属 10元无门槛券
手把手带您无忧上云