js+SVG實現(xiàn)動態(tài)時鐘效果
更新時間:2018年07月14日 15:31:38 作者:rudy24
這篇文章主要為大家詳細(xì)介紹了js+SVG實現(xiàn)動態(tài)時鐘效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js+SVG實現(xiàn)動態(tài)時鐘效果展示的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE HTML>
<html>
<meta charset="utf-8">
<head>
<title>Analog Clock</title>
<script>
function updateTime() {
var now = new Date(); // 當(dāng)前時間
var min = now.getMinutes(); // 分鐘
var hour = (now.getHours() % 12) + min/60; // 轉(zhuǎn)行成可以在時鐘上表示的時間
var seconds = now.getSeconds(); //秒鐘
var minangle = min*6; // 6度表示一分鐘
var hourangle = hour*30; // 30 表示一小時
var secrangel = seconds * 6; // 6度表示一秒鐘
// 獲取表示時鐘時針的SVG元素
var minhand = document.getElementById("minutehand");
var hourhand = document.getElementById("hourhand");
var secondhand = document.getElementById("secondhand");
// 設(shè)置這些元素的SVG屬性,將它們移動到鐘面上
minhand.setAttribute("transform", "rotate(" + minangle + ",50,50)");
hourhand.setAttribute("transform", "rotate(" + hourangle + ",50,50)");
secondhand.setAttribute("transform", "rotate(" + secrangel + ",50,50)");
// 每秒鐘更新下時鐘顯示時間
setTimeout(updateTime, 1000);
}
</script>
<style>
#clock {
stroke: black;
stroke-linecap: round;
fill: #eef;
}
#face { stroke-width: 2px;}
#ticks { stroke-width: 2px; }
#hourhand {stroke-width: 3px;}
#minutehand {stroke-width: 2px;}
#secondhand{stroke-width: 1px;}
#numbers {
font-family: sans-serif; font-size: 7pt; font-weight: bold;
text-anchor: middle; stroke: none; fill: black;
}
</style>
</head>
<body onload="updateTime()">
<!-- viewBox是坐標(biāo)系,width和height是指屏幕大小 -->
<svg id="clock" viewBox="0 0 100 100" width="500" height="500">
<defs> <!-- 定義下拉陰影的濾鏡 -->
<filter id="shadow" x="-50%" y="-50%" width="200%" height="200%">
<feGaussianBlur in="SourceAlpha" stdDeviation="1" result="blur" />
<feOffset in="blur" dx="1" dy="1" result="shadow" />
<feMerge>
<feMergeNode in="SourceGraphic"/><feMergeNode in="shadow"/>
</feMerge>
</filter>
</defs>
<circle id="face" cx="50" cy="50" r="45"/> <!-- 鐘緬 -->
<g id="ticks"> <!-- 12小時的刻度 -->
<line x1='50' y1='5.000' x2='50.00' y2='10.00'/>
<line x1='72.50' y1='11.03' x2='70.00' y2='15.36'/>
<line x1='88.97' y1='27.50' x2='84.64' y2='30.00'/>
<line x1='95.00' y1='50.00' x2='90.00' y2='50.00'/>
<line x1='88.97' y1='72.50' x2='84.64' y2='70.00'/>
<line x1='72.50' y1='88.97' x2='70.00' y2='84.64'/>
<line x1='50.00' y1='95.00' x2='50.00' y2='90.00'/>
<line x1='27.50' y1='88.97' x2='30.00' y2='84.64'/>
<line x1='11.03' y1='72.50' x2='15.36' y2='70.00'/>
<line x1='5.000' y1='50.00' x2='10.00' y2='50.00'/>
<line x1='11.03' y1='27.50' x2='15.36' y2='30.00'/>
<line x1='27.50' y1='11.03' x2='30.00' y2='15.36'/>
</g>
<g id="numbers"> <!-- 標(biāo)記重要的幾個刻度值-->
<text x="50" y="18">12</text><text x="85" y="53">3</text>
<text x="50" y="88">6</text><text x="15" y="53">9</text>
</g>
<!-- 初始繪制成豎直的指針,之后通過js來做旋轉(zhuǎn) -->
<g id="hands" filter="url(#shadow)"> <!-- 給指針添加陰影 -->
<line id="hourhand" x1="50" y1="50" x2="50" y2="25"/>
<line id="minutehand" x1="50" y1="50" x2="50" y2="18"/>
<line id="secondhand" x1="50" y1="50" x2="50" y2="11"/>
</g>
</svg>
</body>
</html>
更多JavaScript時鐘特效點擊查看:JavaScript時鐘特效專題
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
addeventlistener監(jiān)聽scroll跟touch(實例講解)
下面小編就為大家?guī)硪黄猘ddeventlistener監(jiān)聽scroll跟touch(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
用javascript實現(xiàn)自動輸出網(wǎng)頁文本
這篇文章主要介紹了用javascript實現(xiàn)自動輸出網(wǎng)頁文本,用到兩個函數(shù):setTimeout(),遞歸和String.substring();,需要的朋友可以參考下2015-07-07
JavaScript+canvas實現(xiàn)框內(nèi)跳動小球
這篇文章主要為大家詳細(xì)介紹了JavaScript+canvas實現(xiàn)框內(nèi)跳動小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

