-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.html
More file actions
104 lines (103 loc) · 3.14 KB
/
timer.html
File metadata and controls
104 lines (103 loc) · 3.14 KB
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
101
102
103
104
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SVG与多彩渐变圆环倒计时效果</title>
<style type="text/css">
svg {
transform: rotate(-0.05deg);
}
circle {
transition: stroke-dasharray .2s;
}
.time-count-x {
line-height: 1.5;
width: 440px;
position: relative;
}
.time-second {
position: absolute;
top: 50%; left: 0; right: 0;
margin-top: -.75em;
text-align: center;
font-size: 100px;
}
</style>
</head>
<body>
<div id="main">
<div id="body">
<div id="effect" class="part" style="width:45%;">
<h3>效果:</h3>
<div class="show">
<div class="demo">
<div id="timeCountX" class="time-count-x">
<svg width="440" height="440" viewBox="0 0 440 440" class="center">
<defs>
<linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient1">
<stop offset="0%" stop-color="#e52c5c"></stop>
<stop offset="100%" stop-color="#ab5aea"></stop>
</linearGradient>
<linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient2">
<stop offset="0%" stop-color="#4352f3"></stop>
<stop offset="100%" stop-color="#ab5aea"></stop>
</linearGradient>
</defs>
<g transform="matrix(0,-1,1,0,0,440)">
<circle cx="220" cy="220" r="170" stroke-width="50" stroke="#f0f1f5" fill="none" stroke-linecap="round" stroke-dasharray="1069 1069"></circle>
<circle cx="220" cy="220" r="170" stroke-width="50" stroke="url('#gradient1')" stroke-linecap="round" fill="none" stroke-dasharray="1069 1069"></circle>
<circle cx="220" cy="220" r="170" stroke-width="50" stroke="url('#gradient2')" stroke-linecap="round" fill="none" stroke-dasharray="534.5 1069"></circle>
</g>
</svg>
<span id="timeSecond" class="time-second"></span>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
const eleCircles=document.querySelectorAll("#timeCountX circle")
const econstimeSec=document.getElementById("timeSecond")
const perimeter=Math.PI*2*170
const circleInit=function(){
if(eleCircles[1]){
eleCircles[1].setAttribute("stroke-dasharray","1069 1069")
}
if(eleCircles[2]){
eleCircles[2].setAttribute("stroke-dasharray",perimeter/2+" 1069")
}
econstimeSec.innerHTML=""
}
let timerTimeCount=null
const fnTimeCount=function(time){
if(timerTimeCount){
return
}
let b=time||10
const a=function(){
const c=b/15
if(eleCircles[1]){
eleCircles[1].setAttribute("stroke-dasharray",perimeter*c+" 1069")
}
if(eleCircles[2]&&b<=8){
eleCircles[2].setAttribute("stroke-dasharray",perimeter*c+" 1069")
}
if(econstimeSec){
econstimeSec.innerHTML=b
}
b--
if(b<0){
clearInterval(timerTimeCount)
timerTimeCount=null
alert("时间到!")
circleInit()
}
}
a()
timerTimeCount=setInterval(a,1000)
}
fnTimeCount(15)
</script>
</body>
</html>