效果展示
代码实战
方案一
效果预览
原理解析
修改背景的定位,生成动画
编码实战
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>border ranbom</title>
<style>
img {
width: 380px;
}
.box {
/* padding: 4px; */
display: flex;
align-items: center;
justify-content: center;
height: 400px;
width: 400px;
margin: auto;
border: solid 1px;
text-align: center;
}
.horse_run {
background-image: linear-gradient(90deg,
rgba(196, 233, 64, 0) 0%,
green 40%,
orange 40% 70%,
red 70% 100%),
linear-gradient(0deg,
red 30%,
orange 30% 60%,
green 60%,
rgba(196, 233, 64, 0) 100%),
linear-gradient(-90deg,
rgba(196, 233, 64, 0) 0%,
green 40%,
orange 40% 70%,
red 70% 100%),
linear-gradient(0deg,
rgba(196, 233, 64, 0) 0%,
green 40%,
orange 40% 70%,
red 70% 100%);
background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
background-size: 100px 4px, 4px 100px, 100px 4px, 4px 100px;
background-position: -100px 0px, calc(100% - 0px) -100px, calc(100% + 100px) calc(100% - 0px), 0px 0px;
animation: moveLine 6s infinite linear;
/* height: calc(100% - 2px); */
/* padding: 0px; */
background-clip: content-box;
}
@keyframes moveLine {
0% {
background-position: -100px 0px, calc(100% - 0px) -100px, calc(100% + 100px) calc(100% - 0px), 0px 0px;
}
5% {
background-position: 0px 0px, calc(100% - 0px) -100px, calc(100% + 100px) calc(100% - 0px), 0px -100px;
}
30% {
background-position: 100% 0px, calc(100% - 0px) -100px, calc(100% + 100px) calc(100% - 0px), 0px -100px;
}
35% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) 0px, calc(100% + 100px) calc(100% - 0px), 0px -100px;
}
50% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) 100%, calc(100% + 100px) calc(100% - 0px), -100px -100px;
}
55% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) calc(100% + 100px), 100% calc(100% - 0px), -100px calc(100% + 100px);
}
80% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) calc(100% + 100px), 0px calc(100% - 0px), 0px calc(100% + 100px);
}
85% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) calc(100% + 100px), -100px calc(100% - 0px), 0px 100%;
}
100% {
background-position: calc(100% + 100px) 0px, calc(100% - 0px) calc(100% + 100px), -100px calc(100% - 0px), 0px 0px;
}
}
</style>
</head>
<body>
<div class="box horse_run">
<img alt="图片"
src="https://profile-avatar.csdnimg.cn/16a636bfaafa4441b119d1c92e27651e_tianxintiandisheng.jpg!1">
</div>
</body>
</html>
方案二
效果预览
原理
大盒子超出隐藏,利用伪类设置背景颜色,通过动画旋转,并在盒子里面设置一个遮罩层,遮罩层比大盒子小,露出边框达到效果
此方案还有色彩的重叠,但是有个小缺点,旋转的时候有斜边,无斜边的见方案三
编码实战
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>border ranbom</title>
<style>
body {
display: flex;
justify-content: center;
}
.div3 {
width: 300px;
height: 300px;
position: relative;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
img {
position: relative;
z-index: 10;
width: 280px;
}
}
.div3::after {
content: '';
position: absolute;
left: -50%;
top: -50%;
width: 200%;
height: 200%;
background-repeat: no-repeat;
background-size: 60% 60%, 40% 40%;
background-position: 0 0, 100% 0, 100% 100%, 0 100%;
background-image:
linear-gradient(to left, #27FE03, rgba(255, 255, 255, 0)),
linear-gradient(#814FFE, #814FFE),
linear-gradient(#814FFE, #814FFE),
linear-gradient(#814FFE, #814FFE),
linear-gradient(#814FFE, #814FFE);
animation: div3Rotate 3s linear infinite;
}
.div3>div {
position: absolute;
left: 5px;
top: 5px;
width: calc(100% - 10px);
height: calc(100% - 10px);
background: #ffffff;
z-index: 2;
padding: 20px;
box-sizing: border-box;
}
@keyframes div3Rotate {
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div class="div3">
<div>
大盒子超出隐藏,利用伪类设置背景颜色,通过动画旋转,
并在盒子里面设置一个遮罩层,遮罩层比大盒子小,露出边框达到效果
</div>
</div>
</body>
</html>
方案三
效果预览
原理解析
使用clip-path
裁剪边框,形成动画
clip-path 本身是可以进行坐标点的动画的,从一个裁剪形状变换到另外一个裁剪形状。
编码实战
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>border ranbom</title>
<style>
body{
display: flex;
align-items: center;
justify-content: center;
}
.div5 {
width: 219px;
height: 219px;
position: relative;
color: #fff;
border: 6px solid #814FFE;
background: #ffd700;
transition: all .3s;
}
.div5::before {
content: "";
position: absolute;
top: -6px;
left: -6px;
right: -6px;
bottom: -6px;
border: 6px solid;
border-image-slice: 1;
animation: div5Ani 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
border-image-source: linear-gradient(to left, #27FE03, rgba(255, 255, 255, 0));
clip-path: inset(0 0 98% 0);
}
@keyframes div5Ani {
0%,
100% {
border-image-source: linear-gradient(to bottom, #27FE03, rgba(255, 255, 255, 0));
clip-path: inset(0 98% 0 0);
}
25% {
border-image-source: linear-gradient(to left, #27FE03, rgba(255, 255, 255, 0));
clip-path: inset(0 0 98% 0);
}
50% {
border-image-source: linear-gradient(to top, #27FE03, rgba(255, 255, 255, 0));
clip-path: inset(0 0 0 97%);
}
75% {
border-image-source: linear-gradient(to right, #27FE03, rgba(255, 255, 255, 0));
clip-path: inset(98% 0 0 0);
}
}
</style>
</head>
<body>
<div class="div5">
</div>
</body>
</html>