HTML
<div class="container">
<a href="#">
<span></span>
<span></span>
<span></span>
<span></span>
Button
</a>
</div>
CSS
body {
margin: 0;
padding: 0;
background: #34495e;
font-family: sans-serif;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
a {
position: absolute;
transform: translate(-50%, -50%);
padding: 20px 40px;
color: #3498db;
text-transform: uppercase;
letter-spacing: 2px;
font-size: 26px;
text-decoration: none;
box-shadow: 0 0 40px rgba(0, 0, 0, .5);
/* overflow: hidden; */
}
a::before {
content: '';
position: absolute;
top: 2px;
left: 2px;
bottom: 2px;
width: 50%;
background: rgba(255, 255, 255, .2);
}
a:hover span:nth-child(1) {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 2px;
background: linear-gradient(to right, #34495e,#3498db);
animation: span1 2s linear infinite;
}
@keyframes span1 {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
}
a:hover span:nth-child(2) {
position: absolute;
top: 0;
right: 0;
width: 2px;
height: 100%;
background: linear-gradient(to bottom, #34495e,#3498db);
animation: span2 2s linear infinite;
}
@keyframes span2 {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(100%);
}
}
a:hover span:nth-child(3) {
position: absolute;
bottom: 0;
right: 0;
width: 100%;
height: 2px;
background: linear-gradient(to left, #34495e,#3498db);
animation: span3 2s linear infinite;
}
@keyframes span3 {
0% {
transform: translate(100%);
}
100% {
transform: translateX(-100%);
}
}
a:hover span:nth-child(4) {
position: absolute;
bottom: 0;
left: 0;
width: 2px;
height: 100%;
background: linear-gradient(to top, #34495e,#3498db);
animation: span4 2s linear infinite;
}
@keyframes span4 {
0% {
transform: translateY(100%);
}
100% {
transform: translateY(-100%);
}
}
Demo
Description
nth-child(n)
: 选择对应的第 n 个元素
background: linear-gradient
: 背景色设置渐变色
animation: span1 2s linear infinite;
: 设置动画 持续 2s 速度恒定 无限循环
动画效果,从 X 为-100% 位置-> 100%
@keyframes span1 {
0% {
transform: translateX(-100%);
}
100% {
transform: translateX(100%);
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于