呈现效果
正常显示
侧面看的时候
轮播旋转的时候
第一步 首先写出立方体并把他们拼接到一起 因为每一个li都是一个面 给每一个面添加上图片
效果如下
第二步 使用供选择器 再把每一个面的背景图调整到响应的位置(调整的位置取决于你每一个立方体的宽度ul)
效果如下
这样每个面的图片都拼成了一个完整的图
第三步骤 书写js代码 用js操作立方体的 rotate属性进行旋转 transition-delay进行延迟(因为要让他一个一个的旋转)
完整代码如下
可复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
html {
height: 100%;
}
.container {
width: 400px;
margin: 200px auto;
height: 100px;
position: relative;
}
.box {
width: 100px;
height: 100px;
position: absolute;
float: left;
}
#rigth {
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
right: 0px;
top: 50%;
transform: translateY(-50%);
}
#left {
position: absolute;
left: 0px;
background-color: rgba(0, 0, 0, 0.3);
top: 50%;
transform: translateY(-50%);
}
span:hover {
cursor: pointer;
color: red;
}
ul {
width: 100px;
height: 100px;
transform-style: preserve-3d;
float: left;
transition: all 1s;
}
.top {
background-color: red;
transform: translateY(50px) rotateX(-90deg);
transform: translateY(50px) rotateX(-90deg);
background: url('./4.jpg') no-repeat;
}
.footer {
transform: translateY(-50px) rotateX(90deg);
background: url('./2.jpg') no-repeat;
}
.left {
transform: translateX(50px) rotateY(90deg)
}
.rigth {
background-color: yellow;
transform: translateX(-50px) rotateY(-90deg);
}
.Front {
background-color: pink;
transform: translateZ(50px);
background: url('./1.jpeg') no-repeat;
}
.behind {
background-color: blue;
transform: translateZ(-50px) rotateX(-180deg);
background: url('./3.jpg') no-repeat;
}
/* 前 */
.container ul:nth-child(2) .Front {
background-position: -100px 0;
}
.container ul:nth-child(3) .Front {
background-position: -200px 0;
}
.container ul:nth-child(4) .Front {
background-position: -300px 0;
}
/* 底部 */
.container ul:nth-child(2) .footer {
background-position: -100px 0;
}
.container ul:nth-child(3) .footer {
background-position: -200px 0;
}
.container ul:nth-child(4) .footer {
background-position: -300px 0;
}
/* 后面 */
.container ul:nth-child(2) .behind {
background-position: -100px 0;
}
.container ul:nth-child(3) .behind {
background-position: -200px 0;
}
.container ul:nth-child(4) .behind {
background-position: -300px 0;
}
/* 上面 */
.container ul:nth-child(2) .top {
background-position: -100px 0;
}
.container ul:nth-child(3) .top {
background-position: -200px 0;
}
.container ul:nth-child(4) .top {
background-position: -300px 0;
}
</style>
</head>
<body>
<div class="container">
<ul>
<li class="box top"></li>
<li class="box footer"></li>
<li class="box left"></li>
<li class="box rigth"></li>
<li class="box Front"></li>
<li class="box behind"></li>
</ul>
<ul>
<li class="box top"></li>
<li class="box footer"></li>
<li class="box left"></li>
<li class="box rigth"></li>
<li class="box Front"></li>
<li class="box behind"></li>
</ul>
<ul>
<li class="box top"></li>
<li class="box footer"></li>
<li class="box left"></li>
<li class="box rigth"></li>
<li class="box Front"></li>
<li class="box behind"></li>
</ul>
<ul>
<li class="box top"></li>
<li class="box footer"></li>
<li class="box left"></li>
<li class="box rigth"></li>
<li class="box Front"></li>
<li class="box behind"></li>
</ul>
<span id="rigth">下张</span>
<span href="JavaScript:;" id="left">上张</span>
</div>
</body>
<script>
var rigth = document.getElementById("rigth")
var left = document.getElementById("left")
var ul = document.querySelectorAll(".container ul")
var index = 0
var flags = false
rigth.onclick = function () {
if (flags == false) {
flags = true
index++
for (var i = 0; i < ul.length; i++) {
ul[i].style.transform = "rotateX(" + index * 90 + "deg)"
ul[i].style.transitionDelay = "" + i * 0.3 + "s"
}
setTimeout(function () {
flags = false
}, 2000)
}
}
left.onclick = function () {
if (flags == false) {
flags = true
index--
for (var i = 0; i < ul.length; i++) {
ul[i].style.transform = "rotateX(" + index * 90 + "deg)"
ul[i].style.transitionDelay = "" + i * 0.3 + "s"
}
setTimeout(function () {
flags = false
}, 2000)
}
}
</script>
</html>










网友评论