若干张图片绕圈运动,图片自身不旋转 animate transform translate rotate
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>环形旋转效果</title>
<style>
.container {
margin: 200px auto;
width: 300px;
height: 300px;
border: 1px solid black;
border-radius: 1000px;
position: relative;
animation: rotate linear 10s infinite;
}
.container .item {
top: 100px;
left: 100px;
position: absolute;
}
img {
width: 100px;
height: 100px;
border-radius: 50%;
animation: rotate linear 10s infinite reverse;
}
@keyframes rotate {
0%{transform: rotate(0);}
100%{transform: rotate(360deg);}
}
</style>
</head>
<body>
<div class="container">
<div class="item"><img src="./baiye/1 (1).png" alt="" /></div>
<div class="item"><img src="./baiye/1 (2).png" alt="" /></div>
<div class="item"><img src="./baiye/1 (3).png" alt="" /></div>
<div class="item"><img src="./baiye/1 (1).jfif" alt="" /></div>
<div class="item"><img src="./baiye/1 (1).jpg" alt="" /></div>
</div>
<script>
const items = document.querySelectorAll(".container .item");
const r = document.querySelector(".container").clientWidth / 2;
const count = items.length;
const pieceDeg = 360 / count;
for (let i = 0; i < count; i++) {
let t = i * pieceDeg;
t = t * (Math.PI / 180);
const x = Math.sin(t) * r;
const y = -Math.cos(t) * r;
items[i].style.transform = `translate(${x}px,${y}px)`;
}
</script>
</body>
</html>
|