JS html時(shí)鐘制作代碼分享
時(shí)鐘效果圖:
閑來(lái)無(wú)聊做了一個(gè)網(wǎng)頁(yè)的時(shí)鐘,效果模擬傳統(tǒng)時(shí)鐘的運(yùn)行方式,
運(yùn)用了html 的畫布實(shí)現(xiàn)指針,背景圖片引用了網(wǎng)絡(luò)圖片。
具體原理:
首先將時(shí)鐘分為四個(gè)不同區(qū)域,對(duì)每個(gè)區(qū)域計(jì)算cos,sin 來(lái)確實(shí)指針頂點(diǎn)位置。在通過(guò)畫布來(lái)繪畫出指針。
通過(guò)setInterval 每秒刷新指針位置實(shí)現(xiàn) 傳統(tǒng)機(jī)械表針的動(dòng)態(tài)跳動(dòng)。
本人是JS開發(fā)程序員,從業(yè)1年多。閑來(lái)無(wú)聊的簡(jiǎn)單頁(yè)面,
歡迎大家提問(wèn),或者建議。共同進(jìn)步
代碼部分,直接復(fù)制HTML 文件中即可查看效果:
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>clock</title> </head> <body style="color:green; <!--background-image:url('http://image.lxway.com/upload/b/f0/bf0d97dcee487096548e6edbc89d4963_thumb.png');--> background-size:100%; background-repeat: no-repeat; background-attachment:fixed"> <div style="width: 900px; height: 900px; margin-top: 50px; margin-left: 50px;"> <div style="position: absolute; left:119px;top: 193px; width: 900px; height: 900px; background-image:url('http://image.lxway.com/upload/b/f0/bf0d97dcee487096548e6edbc89d4963_thumb.png'); background-repeat: no-repeat; z-index: -1;"> </div> <canvas id="t" width="800" height="800"></canvas> <div style="position: absolute; left:118px;top: 464px; width: 100px; height: 200px; background-color: white; background-repeat: no-repeat; z-index: 10;"> </div> </div> <script language="javascript"> var s = setInterval(moveI, 1000); function moveI() { var c = document.getElementById("t"); var pc = c.getContext("2d"); c.height = c.height; //秒 pc.lineWidth = 3; pc.strokeStyle = 'rgba(255,0,0,0.8)'; var now = new Date(); var sindex = getxy(150, now.getSeconds()); pc.moveTo(400, 400); pc.lineTo(sindex.x, sindex.y); pc.stroke(); pc.beginPath(); //分 pc.lineWidth = 7; pc.strokeStyle = 'rgba(50,50,50,0.8)'; var mindex = getxy(120, now.getMinutes() + (now.getSeconds() / 60)); pc.moveTo(400, 400); pc.lineTo(mindex.x, mindex.y); pc.stroke(); pc.beginPath(); //時(shí) pc.lineWidth = 10; pc.strokeStyle = 'rgba(0,0,0,0.8)'; var hindex = getxy(80, ((now.getHours() > 12 ? now.getHours() - 12 : now.getHours()) + (now.getMinutes() / 60)) * 5); pc.moveTo(400, 400); pc.lineTo(hindex.x, hindex.y); pc.stroke(); }; function getxy(r, t) { //計(jì)算分區(qū) 0,1,2,3 var a = parseInt(t / 15); //分區(qū)角度 t = t - 15 * a; var y; var x; //基于分區(qū)的坐標(biāo)計(jì)算 switch (a) { case 0: y = r - (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15))); x = r + (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15))); break; case 1: y = r + (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15))); x = r + (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15))); break; case 2: y = r + (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15))); x = r - (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15))); break; case 3: y = r - (r * Math.sin(2 * Math.PI / 360 * 90 * (t / 15))); x = r - (r * Math.cos(2 * Math.PI / 360 * 90 * (t / 15))); break; default: break; } y = (400 - r) + y; x = (400 - r) + x; return { 'x': x, 'y': y }; }; </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 五步輕松實(shí)現(xiàn)JavaScript HTML時(shí)鐘效果
- JS+Canvas繪制動(dòng)態(tài)時(shí)鐘效果
- JavaScript之創(chuàng)意時(shí)鐘項(xiàng)目(實(shí)例講解)
- 一個(gè)簡(jiǎn)易時(shí)鐘效果js實(shí)現(xiàn)代碼
- 基于JavaScript實(shí)現(xiàn)數(shù)碼時(shí)鐘效果
- js實(shí)現(xiàn)一個(gè)簡(jiǎn)單的數(shù)字時(shí)鐘效果
- JS實(shí)現(xiàn)簡(jiǎn)易刻度時(shí)鐘示例代碼
- js Canvas繪制圓形時(shí)鐘效果
- JS+CSS實(shí)現(xiàn)滾動(dòng)數(shù)字時(shí)鐘效果
相關(guān)文章
Mysql內(nèi)儲(chǔ)存JSON字符串根據(jù)條件進(jìn)行查詢
本文主要介紹了Mysql內(nèi)儲(chǔ)存JSON字符串根據(jù)條件進(jìn)行查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02JavaScript實(shí)現(xiàn)二叉樹層序遍歷
這篇文章主要為大家簡(jiǎn)單介紹一下JS中如何實(shí)現(xiàn)二叉樹層序遍歷,感興趣的小伙伴可以詳細(xì)參考閱讀2023-03-03JavaScript制作windows經(jīng)典掃雷小游戲
掃雷是一款相當(dāng)大眾的小游戲,游戲目標(biāo)是在最短的時(shí)間內(nèi)根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時(shí)避免踩雷。今天我們來(lái)看看如何使用javascript來(lái)實(shí)現(xiàn)這款小游戲2015-03-03基于Web標(biāo)準(zhǔn)的UI組件 — 樹狀菜單(2)
基于Web標(biāo)準(zhǔn)的UI組件 — 樹狀菜單(2)...2006-09-09詳解用函數(shù)式編程對(duì)JavaScript進(jìn)行斷舍離
本篇文章主要介紹了用函數(shù)式編程對(duì)JavaScript進(jìn)行斷舍離,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09javascript實(shí)現(xiàn)日期按月份加減
JavaScript實(shí)現(xiàn)日期加減計(jì)算功能代碼實(shí)例,因?yàn)樵趈s中沒有類似C#中的AddDays方法,所以要想實(shí)現(xiàn)日期加減的話,就需要自己寫函數(shù)來(lái)實(shí)現(xiàn)。這里分享給大家,有需要的小伙伴可以參考下2015-05-05JavaScript實(shí)現(xiàn)文件的拖拽上傳功能
文件上傳,可以說(shuō)是我們?cè)陧?xiàng)目中最常用的功能之一,文件上傳一般有兩種形式:點(diǎn)擊上傳和拖拽上傳,而上傳的內(nèi)容,又大體包括:文件和文件夾,本文給大家介紹了JavaScript實(shí)現(xiàn)文件的拖拽上傳功能的方法,需要的朋友可以參考下2024-02-02微信開發(fā)之調(diào)起攝像頭、本地展示圖片、上傳下載圖片實(shí)例
這篇文章主要介紹了微信開發(fā)之調(diào)起攝像頭、本地展示圖片、上傳下載圖片實(shí)例,具有一定的參考價(jià)值有興趣的可以了解一下。2016-12-12