如下效果图
实现原理
通过CSS3D属性,定位图片位置,通过动画让盒子一直旋转。
- 新建一个ul,内置6个li。每个li加一个背景图片。
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
-
全部定位到一个位置。页面上显示的2D页面。也就是跑马灯的正面。
-
从顶部看走马灯,就是一个六边形,只不过六个边是一个立体的面,也就是最下边的那条边
-
以上图为准,
-
由于li定位在一个位置,正面为图片,反面空白,当跑马灯旋转到第2张图片看到的应该是正面.
-
第1张图片,不需要旋转,直接位移Z轴正方向600px。
transform: translateZ(600px);
- 第2张图片,位移Z轴负方向600px,旋转180deg,
切记,旋转与位移顺序不要写反了
transform: translateZ(-600px) rotateY(180deg);
- 4与6旋转角度相同。
图片6 transform: rotateY(60deg) translateZ(-600px);
图片4
transform: rotateY(60deg) translateZ(600px);
- 3与5旋转角度相同。
图片3 transform: rotateY(-60deg) translateZ(600px);
图片5
transform: rotateY(-60deg) translateZ(-600px);
- 添加3D展示。
transform-style: preserve-3d;
-给ul添加动画,让它动起来,代码如下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-image: linear-gradient(76.8deg, rgb(121, 45, 129) 3.6%, rgb(36, 31, 98) 90.4%);
}
ul {
list-style: none;
width: 400px;
height: 400px;
margin: 0 auto;
margin-top: 200px;
position: relative;
transform-style: preserve-3d;
transform: rotateX(-10deg);
animation: spin 6s infinite linear;
}
@keyframes spin {
to {
transform: rotateX(-10deg) rotateY(360deg);
}
}
ul:hover {
animation-play-state: paused;
}
li {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
li:nth-child(1) {
transform: translateZ(600px);
background: url(./img/pic-1.jpg) no-repeat;
background-size: 400px 400px;
}
li:nth-child(2) {
transform: translateZ(-600px) rotateY(180deg);
background: url(./img/pic-2.jpg)no-repeat;
background-size: 400px 400px;
}
li:nth-child(3) {
transform: rotateY(60deg) translateZ(-600px);
background: url(./img/pic-3.jpg)no-repeat;
background-size: 400px 400px;
}
li:nth-child(4) {
transform: rotateY(60deg) translateZ(600px);
background: url(./img/pic-4.jpg)no-repeat;
background-size: 400px 400px;
}
li:nth-child(5) {
transform: rotateY(-60deg) translateZ(-600px);
background: url(./img/pic-5.jpg)no-repeat;
background-size: 400px 400px;
}
li:nth-child(6) {
transform: rotateY(-60deg) translateZ(600px);
background: url(./img/pic-6.jpg)no-repeat;
background-size: 400px 400px;
}
</style>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>