旋转相册制作css

旋转相册制作CSS
旋转相册通常通过CSS动画和3D变换实现,以下是一个完整的实现方法:
基础HTML结构
<div class="photo-album">
<div class="photo"></div>
<div class="photo"></div>
<div class="photo"></div>
<!-- 更多照片 -->
</div>
核心CSS样式
.photo-album {
width: 300px;
height: 200px;
position: relative;
perspective: 1000px;
margin: 50px auto;
}
.photo {
position: absolute;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
border-radius: 5px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
transform-style: preserve-3d;
transition: transform 1s ease;
}
3D旋转动画
@keyframes rotate {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
.photo-album {
animation: rotate 20s infinite linear;
}
照片位置计算
对于包含多张照片的旋转相册,需要使用CSS计算每张照片的旋转角度:
.photo {
transform: rotateY(calc(var(--i) * (360deg / var(--total))))
translateZ(250px);
}
/* 示例:6张照片 */
.photo:nth-child(1) { --i: 0; --total: 6; }
.photo:nth-child(2) { --i: 1; --total: 6; }
/* 继续为每张照片设置变量 */
悬停暂停效果
.photo-album:hover {
animation-play-state: paused;
}
.photo:hover {
transform: scale(1.2);
z-index: 10;
}
完整示例
<!DOCTYPE html>
<html>
<head>
<style>
.photo-album {
width: 300px;
height: 200px;
position: relative;
perspective: 1000px;
margin: 100px auto;
animation: rotate 20s infinite linear;
}
.photo {
position: absolute;
width: 100%;
height: 100%;
background-size: cover;
background-position: center;
border-radius: 5px;
box-shadow: 0 5px 15px rgba(0,0,0,0.3);
transform-style: preserve-3d;
}
.photo:nth-child(1) {
background-image: url('photo1.jpg');
transform: rotateY(0deg) translateZ(250px);
}
.photo:nth-child(2) {
background-image: url('photo2.jpg');
transform: rotateY(60deg) translateZ(250px);
}
.photo:nth-child(3) {
background-image: url('photo3.jpg');
transform: rotateY(120deg) translateZ(250px);
}
/* 添加更多照片 */
@keyframes rotate {
0% { transform: rotateY(0deg); }
100% { transform: rotateY(360deg); }
}
.photo-album:hover {
animation-play-state: paused;
}
.photo:hover {
transform: scale(1.2) translateZ(250px);
}
</style>
</head>
<body>
<div class="photo-album">
<div class="photo"></div>
<div class="photo"></div>
<div class="photo"></div>
<!-- 更多照片 -->
</div>
</body>
</html>
注意事项
- 调整
perspective值可以改变3D效果的强度 translateZ值决定照片距离旋转中心的距离- 动画持续时间可以根据需要调整
- 确保照片数量与旋转角度计算匹配
- 为获得最佳效果,建议使用6-12张照片







