JavaScript圓形鐘錶效果代碼實例

分享一段代碼實例,它實現了利用js實現了自動讀取本地時間的圓形鐘錶效果。

代碼實例如下:

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>圓形鐘錶</title>
<style type="text/css">
#outbox {
  position: relative;
  border-radius: 50%;
  width: 400px;
  height: 400px;
  margin: 150px auto;
  background: linear-gradient(#D1E6ED, #767775);
}
#innerbox {
  position: absolute;
  width: 360px;
  height: 360px;
  border-radius: 50%;
  margin: 20px;
  background-color: white;
  box-shadow: 0 0 10px 5px gray inset;
}
#innerbox > div {
  width: 350px;
  height: 32px;
  font-size: 1.5em;
  line-height: 32px;
  text-align: center;
  position: absolute;
  top: 164px;
  left: 5px;
}
#innerbox > div > span:nth-child(1) {
  float: left;
}
#innerbox > div > span:nth-child(2) {
  float: right;
}
#second_line {
  height: 175px;
  width: 4px;
  background-color: red;
  top: 25px;
  left: 198px;
  position: absolute;
  z-index: 13;
}
#minute_line {
  height: 150px;
  width: 6px;
  background-color: blue;
  top: 50px;
  left: 197px;
  position: absolute;
  z-index: 12;
}
#hour_line {
  height: 130px;
  width: 8px;
  background-color: black;
  top: 70px;
  left: 196px;
  position: absolute;
  z-index: 11;
}
</style>
</head>
<body>
  <div id="outbox">
    <div id="innerbox">
      <div>
        <span>1</span><span>7</span>
      </div>
      <div>
        <span>2</span><span>8</span>
      </div>
      <div>
        <span>3</span><span>9</span>
      </div>
      <div>
        <span>4</span><span>10</span>
      </div>
      <div>
        <span>5</span><span>11</span>
      </div>
      <div>
        <span>6</span><span>12</span>
      </div>
    </div>
    <div id="second_line">
    </div>
    <div id="minute_line">
    </div>
    <div id="hour_line">
    </div>
  </div>
</body>
<script>
var hours, minutes, seconds;
//獲取所有元素
var arrDiv = document.querySelectorAll("#innerbox>div");
var arrSpan = document.querySelectorAll("#innerbox>div>span");
  
//佈局
for (var i = 0; i < arrDiv.length; i++) {
  arrDiv[i].style.transform = "rotateZ(-" + (240 - 30 * i) + "deg)";
}
  
for (var i = 0; i < arrSpan.length; i += 2) {
  arrSpan[i].style.transform = "rotateZ(" + (240 - 15 * i) + "deg)";
  arrSpan[i + 1].style.transform = "rotateZ(" + (240 - 15 * i) + "deg)";
}
  
//選擇旋轉點
document.getElementById("second_line").style.transformOrigin = "bottom";
document.getElementById("minute_line").style.transformOrigin = "bottom";
document.getElementById("hour_line").style.transformOrigin = "bottom";
//獲取秒數
seconds = new Date().getSeconds();
//獲取分鐘數,如果seconds=30,相當於分針轉了(30/60)分鐘
minutes = new Date().getMinutes() + seconds / 60;
//獲取小時數,同理可得
hours = new Date().getHours() + minutes / 60;
//計時器
setInterval(init, 1000);
//初始化
function init() {
  hours = hours + 1 / 3600;
  minutes = minutes + 1 / 60;
  seconds++;
  document.getElementById("second_line").style.transform = "rotate(" + 6 * seconds + "deg)";
  document.getElementById("minute_line").style.transform = "rotate(" + 6 * minutes + "deg)";
  document.getElementById("hour_line").style.transform = "rotate(" + 30 * hours + "deg)";
}
init();  
</script>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章