通过扫两html+css+js代码实现滑块掉落的效果,主要用的是css3的animation属性
- 废话不多说,先上效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/74a594a2cb2a4c387b63ecfa3b989379.gif#pic_center)
- html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="css/style.css">
<script src="js/index.js"></script>
</head>
<body>
<!-- 主界面创建一个容器就即可,利用js动态创建滑块 -->
<div class="container"></div>
</body>
</html>
*{
padding: 0;
margin: 0;
}
body{
overflow: hidden;
}
.container{
width: 100%;
height: 100vh;
background: url('../img/bg.jpg') no-repeat center center ;
}
.container span{
display: inline-block;
width: 10%;
height: 41px;
background: white;
border: 1px solid rgba(0, 0, 0);
box-sizing: border-box;
box-shadow: 0 2px 5px rgba(0,0,0,1);
}
.container span.fall{
z-index: 1000;
background: #f00;
transition: 0.2s;
pointer-events: none;
animation: fall 2s linear forwards;
}
@keyframes fall{
0%{
transform: translateY(0) rotate(0deg);
}
100%{
transform: translateY(1000px) rotate(90deg);
}
}
window.onload=function(){
const div=document.getElementsByClassName('container')[0];
for(let i=0;i<160;i++){
let span=document.createElement('span');
div.appendChild(span);
}
const spans=document.getElementsByTagName('span');
const list=Array.from(spans);
list.forEach(element => {
element.onmouseup=function(){
element.setAttribute('class','fall');
}
});
};