EP06 | 跟著影片做前端切版練習 - Glowing Loader Ring Animation

練習影片

練習重點

  • border : 可用來設定元素的邊框,並可針對不同位置的邊框做不同的設定。

  • animation : 製作動畫效果。

  • transform : 可以讓元素被平移(translate)、旋轉(rotate)、縮放(scale)和傾斜(skew)。

結構

HTML

1
2
3
4
5
6
<body>
<div class="circle circle-yellow">
</div>
<div class="circle circle-blue">
</div>
</body>

CSS

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
body {
background: #121212;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position:relative;
}

.circle {
/*讓.circle-yellow跟.circle-blue變成重疊*/
position: absolute;
border-radius: 50%;
border: 5px solid #212121;
animation: circling 4s linear infinite;
}

.circle-yellow {
width: 250px;
height: 250px;
border-top: 5px solid rgb(255, 246, 0);
border-left: 5px solid rgb(255, 246, 0);
}

.circle-blue {
width: 200px;
height: 200px;
border-bottom: 5px solid rgb(0, 255, 255);
border-right: 5px solid rgb(0, 255, 255);
animation: circling 4s reverse linear infinite;
}

.circle::before {
content: "";
width: 15px;
height: 15px;
background: #fff;
position: absolute;
border-radius: 50%;
}

.circle.circle-yellow::before {
background: rgb(255, 246, 0);
/*圓點的top跟right位置需要測試*/
top: 28px;
right: 28px;
box-shadow: 0 0 20px rgb(255, 246, 0),
0 0 40px rgb(255, 246, 0),
0 0 60px rgb(255, 246, 0),
0 0 80px rgb(255, 246, 0),
0 0 0 5px rgba(255, 246, 0,0.1);
}

.circle.circle-blue::before {
background: rgb(0, 255, 255);
/*圓點的top跟right位置需要測試*/
top: 20px;
right: 20px;
box-shadow: 0 0 20px rgb(0, 255, 255),
0 0 40px rgb(0, 255, 255),
0 0 60px rgb(0, 255, 255),
0 0 80px rgb(0, 255, 255),
0 0 0 5px rgba(0, 255, 255,0.1);
}

@keyframes circling {
0%{
transform:rotate(0deg)
}
100%{
transform:rotate(360deg)
}
}

小提醒

錯誤示範:
本來 HTML 結構是用 .circle-yellow 包住 circle circle-blue (如下方),最後在做動畫效果時才發現,原來子元素跟父元素的 transform 動畫不能不一致,所以無法達成外層正向轉,內層反向轉的動畫,所以最後還是讓 .circle-yellowcircle circle-blue 成為兄弟元素,再用 position:absolute 讓兩者重疊再一起,並設定不同的 animation-direction

1
2
3
4
5
6
7
<!--錯誤示範,無法成功運行-->
<body>
<div class="circle circle-yellow">
<div class="circle circle-blue">
</div>
</div>
</body>

Demo:

See the Pen EP06 | 跟著影片做前端切版練習 - Glowing Loader Ring Animation by Celeste6666 (@celeste6666) on CodePen.