CSS旋转border
最近实现一个border围绕内容旋转的动画,效果如下:

实现原理比较简单,借助于css的clip-path属性,它的作用是裁剪元素形成新的可视化区域。详细请看官方文档
首先构建border和内容区域,border需借助after和before属性避免内容被剪切,后使用clip-path裁剪after和before,最后把各个方向裁剪部分做成动画即可。

实现代码如下:
<template>
<div class="page">
<div class="box">
<div class="content"></div>
</div>
</div>
</template>
<script>
</script>
<style scoped lang="scss">
.page {
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
.box {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
background: transparent;
&::after {
content: '';
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
border: 5px solid #00FF00;
box-sizing: border-box;
clip-path: inset(0 0 195px 0);
animation: clipBoderTop 5s linear infinite;
}
&::before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
border: 5px solid #00FF00;
box-sizing: border-box;
clip-path: inset(0 0 195px 0);
animation: clipBoderBottom 5s linear infinite;
}
.content {
left: 5px;
top: 5px;
width: 180px;
height: 180px;
background: #000FFF;
}
}
}
@keyframes clipBoderTop {
from {
clip-path: inset(0 0 195px 0);
}
25% {
clip-path: inset(0 0 0 195px);
}
50% {
clip-path: inset(195px 0 0 0);
}
75% {
clip-path: inset(0 195px 0 0);
}
to {
clip-path: inset(0 0 195px 0);
}
}
@keyframes clipBoderBottom {
from {
clip-path: inset(195px 0 0 0);
}
25% {
clip-path: inset(0 195px 0 0);
}
50% {
clip-path: inset(0 0 195px 0);
}
75% {
clip-path: inset(0 0 0 195px);
}
to {
clip-path: inset(198px 0 0 0);
}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
上次更新: 2025/09/05, 8:09:00