css上下跳动
效果:
html代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="./index.css"/>
</head>
<body>
<div class="loader">
<div style="--i:1"></div>
<div style="--i:2"></div>
<div style="--i:3"></div>
<div style="--i:4"></div>
<div style="--i:5"></div>
</div>
</body>
</html>
css代码:
body,div{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
display:flex;
justify-content: center;
align-items: center;
background-color: #edf1f4;
min-height: 100vh;
}
.loader{
display: flex;
flex-direction: row
}
.loader div {
position: relative;
width: 40px;
height: 200px;
margin: 50px;
box-shadow: 15px 15px 20px rgba(0,0,0,0.1),
-15px -15px 20px #fff,
inset -5px -5px 5px rgba(255,255,255,0.5),
inset 5px 5px 5px rgba(0,0,0,0.05);
border-radius: 40px;
border:2px solid #edf1f4;
background-color: linear-gradient(to bottom,rgba(0,0,0,0.05));
overflow: hidden;
}
.loader div::before{
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border-radius: 40px;
/* background-color: yellow; */
box-shadow: 15px 15px 20px rgba(0,0,0,0.1),
-15px -15px 20px #fff,
inset -5px -5px 5px rgba(255,255,255,0.5),
inset 5px 5px 5px rgba(0,0,0,0.05);
}
.loader div::after{
position: absolute;
content: "";
top:0;
left: 0;
width: 36px;
height: 36px;
border-radius: 50%;
background-color: #fff;
box-shadow: inset -5px -5px 5px rgba(0,0,0,.2),
0 420px 0 400px #2196f3;
animation: animate 2.5s linear infinite;
animation-delay: calc(var(--i) * 0.5s);
}
@keyframes animate{
0%{
transform: translateY(160px);
filter: hue-rotate(180deg);
/* hue-rotate 色彩转换传递的参数为角度,
该角度指向的颜色圆盘上的颜射就是当前显示的颜色 */
}
50%{
transform: translateY(0px);
}
100%{
transform: translateY(160px);
}
}
总结:
在实现上下跳动效果的时,主要用到transform的标签移动,颜色的变化用到了css中的filter层叠样式中的:hue-rotate(),函数hue-rotate()需要传递一个参数,该参数用来指定颜色圆环的颜色,并把该颜色显示出来