CSS3+js實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘特效

學(xué)習(xí)css3ing,正在學(xué)習(xí)transfomr,突發(fā)奇想用此做個(gè)小時(shí)鐘,開(kāi)始吧:
準(zhǔn)備前期工作,把時(shí)鐘的表盤(pán),時(shí)分秒針,實(shí)時(shí)時(shí)間標(biāo)簽 的大概樣子做好,效果如圖:
html代碼如下:
<div class="main">
<div id="timeLabel"></div>
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
css 代碼如下:
<style>
* {
margin: 0;
padding: 0;
}
.main {
position: relative;
margin: 100px auto;
width: 300px;
height: 300px;
border-radius: 300px;
border: 1px solid #000;
box-shadow:2px 5px;
}
#timeLabel {
position: absolute;
background-color:pink;
width:100px;
height:30px;
left:100px;
top:180px;
}
#hour {
width: 100px;
height: 10px;
background-color: red;
position:absolute;
left:150px;
top:145px;
}
#minute {
width:120px;
height:8px;
background-color:blue;
position:absolute;
left:150px;
top:146px;
}
#second {
width: 140px;
height: 4px;
background-color: green;
position: absolute;
left: 150px;
top: 148px;
}
</style>
2. 初始化默認(rèn)時(shí)間,和表盤(pán)刻度 ,效果如下:
更改后的css代碼:
<style>
* {
margin: 0;
padding: 0;
}
.main {
position: relative;
margin: 100px auto;
width: 300px;
height: 300px;
border-radius: 300px;
border: 1px solid #000;
box-shadow: 2px 5px #808080;
}
#timeLabel {
position: absolute;
background-color: pink;
width: 80px;
height: 25px;
left: 110px;
top: 180px;
color: #fff;
line-height: 25px;
text-align: center;
}
#hour {
width: 100px;
height: 10px;
background-color: red;
position: absolute;
left: 150px;
top: 145px;
transform-origin: 0 50%;
}
#minute {
width: 120px;
height: 8px;
background-color: blue;
position: absolute;
left: 150px;
top: 146px;
transform-origin: 0 50%;
}
#second {
width: 140px;
height: 4px;
background-color: green;
position: absolute;
left: 150px;
top: 148px;
transform-origin: 0 50%;
}
.hourPointer, .minuterPointer, .secondPointer {
position: absolute;
transform-origin: 0 50%;
}
.hourPointer {
height: 10px;
width: 12px;
left: 150px;
top: 145px;
background-color: #f00;
z-index:3;
}
.minuterPointer {
height: 8px;
width: 10px;
left: 150px;
top: 146px;
background-color: #b6ff00;
z-index: 2;
}
.secondPointer {
height: 6px;
width: 8px;
left: 150px;
top: 147px;
background-color: #fa8;
z-index: 1;
}
</style>
初始化 js代碼:
window.onload = function () {
initClock();
}
var timer = null;
function $(id) {
return document.getElementById(id)
}
function CreateKeDu(pElement, className, deg, translateWidth) {
var Pointer = document.createElement("div");
Pointer.className = className
Pointer.style.transform = "rotate(" + deg + "deg) translate(" + translateWidth + "px)";
pElement.appendChild(Pointer);
}
function initClock() {
var main = $("biaopan");
var timeLabel = $("timeLabel");
var hour = $("hour");
var minute = $("minute");
var second = $("second");
var now = new Date();
var nowHour = now.getHours();
var nowMinute = now.getMinutes();
var nowSecond = now.getSeconds();
//初始化timeLabel
timeLabel.innerHTML = nowHour + ":" + nowMinute + ":" + nowSecond;
//初始化表盤(pán)
for (var index = 0; index < 4; index++) {
CreateKeDu(main, "hourPointer", index * 90, 138);
}
for (var index = 0; index < 12; index++) {
CreateKeDu(main, "minuterPointer",index*30, 140);
}
for (var index = 0; index < 60; index++) {
CreateKeDu(main, "secondPointer", index * 6, 142);
}
//初始化時(shí)分秒針
second.style.transform = "rotate(" + (nowSecond * 6 - 90) + "deg)";
minute.style.transform = "rotate(" + (nowMinute * 6 + 1 / 10 * nowSecond - 90) + "deg)";
hour.style.transform = "rotate(" + (nowHour * 30 + 1 / 2 * nowMinute + 1 / 120 * nowSecond - 90) + "deg)";
}
3.添加定時(shí)器:
js代碼如下:
//定時(shí)器
function startMove() {
clearInterval(timer);
timer = setInterval(function () {
var now = new Date();
var nowSecond = now.getSeconds();
var nowMinute = now.getMinutes();
var nowHour = now.getHours();
second.style.transform = "rotate(" + (nowSecond * 6 - 90) + "deg)";
minute.style.transform = "rotate(" + (nowMinute * 6 + 1 / 10 * nowSecond - 90) + "deg)";
hour.style.transform = "rotate(" + (nowHour * 30 + 1 / 2 * nowMinute + 1 / 120 * nowSecond - 90) + "deg)";
timeLabel.innerHTML = nowHour + ":" + nowMinute + ":" + nowSecond;
}, 1000);
}
4.使用OOP方式更改:
修改后的js代碼如下:
function Clock() {
//定義屬性
this.main = this.$("biaopan");
this.timeLabel = this.$("timeLabel");
this.hour = this.$("hour");
this.minute = this.$("minute");
this.second = this.$("second");
this.nowHour = null;
this.nowMinute = null;
this.nowSecond = null;
this.timer = null;
var _this = this;
//初始化函數(shù)
var init = function () {
_this.getNowTime();
_this.initClock();
_this.InterVal();
}
init();
}
Clock.prototype.$ = function (id) {
return document.getElementById(id)
}
Clock.prototype.CreateKeDu = function (className, deg, translateWidth) {
var Pointer = document.createElement("div");
Pointer.className = className
Pointer.style.transform = "rotate(" + deg + "deg) translate(" + translateWidth + "px)";
this.main.appendChild(Pointer);
}
Clock.prototype.getNowTime = function () {
var now = new Date();
this.nowHour = now.getHours();
this.nowMinute = now.getMinutes();
this.nowSecond = now.getSeconds();
}
Clock.prototype.setPosition = function () {
this.second.style.transform = "rotate(" + (this.nowSecond * 6 - 90) + "deg)";
this.minute.style.transform = "rotate(" + (this.nowMinute * 6 + 1 / 10 * this.nowSecond - 90) + "deg)";
this.hour.style.transform = "rotate(" + (this.nowHour * 30 + 1 / 2 * this.nowMinute + 1 / 120 * this.nowSecond - 90) + "deg)";
}
Clock.prototype.initClock = function () {
//初始化timeLabel
this.timeLabel.innerHTML = this.nowHour + ":" + this.nowMinute + ":" + this.nowSecond;
//初始化表盤(pán)
for (var index = 0; index < 4; index++) {
this.CreateKeDu("hourPointer", index * 90, 138);
}
for (var index = 0; index < 12; index++) {
this.CreateKeDu("minuterPointer", index * 30, 140);
}
for (var index = 0; index < 60; index++) {
this.CreateKeDu("secondPointer", index * 6, 142);
}
this.setPosition();
}
Clock.prototype.InterVal = function () {
clearInterval(this.timer);
var _this = this;
this.timer = setInterval(function () {
_this.getNowTime();
_this.second.style.transform = "rotate(" + (_this.nowSecond * 6 - 90) + "deg)";
_this.minute.style.transform = "rotate(" + (_this.nowMinute * 6 + 1 / 10 * _this.nowSecond - 90) + "deg)";
_this.hour.style.transform = "rotate(" + (_this.nowHour * 30 + 1 / 2 * _this.nowMinute + 1 / 120 * _this.nowSecond - 90) + "deg)";
_this.timeLabel.innerHTML = _this.nowHour + ":" + _this.nowMinute + ":" + _this.nowSecond;
}, 1000);
}
最后調(diào)用如下:
window.onload = function () {
new Clock();
}
最終頁(yè)面代碼:
<!DOCTYPE html>
<html xmlns="<a >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.main {
position: relative;
margin: 100px auto;
width: 300px;
height: 300px;
border-radius: 300px;
border: 1px solid #000;
box-shadow: 2px 5px #808080;
}
#timeLabel {
position: absolute;
background-color: pink;
width: 80px;
height: 25px;
left: 110px;
top: 180px;
color: #fff;
line-height: 25px;
text-align: center;
}
#hour {
width: 100px;
height: 10px;
background-color: red;
position: absolute;
left: 150px;
top: 145px;
transform-origin: 0 50%;
}
#minute {
width: 120px;
height: 8px;
background-color: blue;
position: absolute;
left: 150px;
top: 146px;
transform-origin: 0 50%;
}
#second {
width: 140px;
height: 4px;
background-color: green;
position: absolute;
left: 150px;
top: 148px;
transform-origin: 0 50%;
}
.hourPointer, .minuterPointer, .secondPointer {
position: absolute;
transform-origin: 0 50%;
}
.hourPointer {
height: 10px;
width: 12px;
left: 150px;
top: 145px;
background-color: #f00;
z-index: 3;
}
.minuterPointer {
height: 8px;
width: 10px;
left: 150px;
top: 146px;
background-color: #b6ff00;
z-index: 2;
}
.secondPointer {
height: 6px;
width: 8px;
left: 150px;
top: 147px;
background-color: #fa8;
z-index: 1;
}
</style>
<script>
function Clock() {
//定義屬性
this.main = this.$("biaopan");
this.timeLabel = this.$("timeLabel");
this.hour = this.$("hour");
this.minute = this.$("minute");
this.second = this.$("second");
this.nowHour = null;
this.nowMinute = null;
this.nowSecond = null;
this.timer = null;
var _this = this;
//初始化函數(shù)
var init = function () {
_this.getNowTime();
_this.initClock();
_this.InterVal();
}
init();
}
Clock.prototype.$ = function (id) {
return document.getElementById(id)
}
Clock.prototype.CreateKeDu = function (className, deg, translateWidth) {
var Pointer = document.createElement("div");
Pointer.className = className
Pointer.style.transform = "rotate(" + deg + "deg) translate(" + translateWidth + "px)";
this.main.appendChild(Pointer);
}
Clock.prototype.getNowTime = function () {
var now = new Date();
this.nowHour = now.getHours();
this.nowMinute = now.getMinutes();
this.nowSecond = now.getSeconds();
}
Clock.prototype.setPosition = function () {
this.second.style.transform = "rotate(" + (this.nowSecond * 6 - 90) + "deg)";
this.minute.style.transform = "rotate(" + (this.nowMinute * 6 + 1 / 10 * this.nowSecond - 90) + "deg)";
this.hour.style.transform = "rotate(" + (this.nowHour * 30 + 1 / 2 * this.nowMinute + 1 / 120 * this.nowSecond - 90) + "deg)";
}
Clock.prototype.initClock = function () {
//初始化timeLabel
this.timeLabel.innerHTML = this.nowHour + ":" + this.nowMinute + ":" + this.nowSecond;
//初始化表盤(pán)
for (var index = 0; index < 4; index++) {
this.CreateKeDu("hourPointer", index * 90, 138);
}
for (var index = 0; index < 12; index++) {
this.CreateKeDu("minuterPointer", index * 30, 140);
}
for (var index = 0; index < 60; index++) {
this.CreateKeDu("secondPointer", index * 6, 142);
}
this.setPosition();
}
Clock.prototype.InterVal = function () {
clearInterval(this.timer);
var _this = this;
this.timer = setInterval(function () {
_this.getNowTime();
_this.second.style.transform = "rotate(" + (_this.nowSecond * 6 - 90) + "deg)";
_this.minute.style.transform = "rotate(" + (_this.nowMinute * 6 + 1 / 10 * _this.nowSecond - 90) + "deg)";
_this.hour.style.transform = "rotate(" + (_this.nowHour * 30 + 1 / 2 * _this.nowMinute + 1 / 120 * _this.nowSecond - 90) + "deg)";
_this.timeLabel.innerHTML = _this.nowHour + ":" + _this.nowMinute + ":" + _this.nowSecond;
}, 1000);
}
window.onload = function () {
new Clock();
}
</script>
</head>
<body>
<div class="main" id="biaopan">
<div id="timeLabel"></div>
<div id="hour"></div>
<div id="minute"></div>
<div id="second"></div>
</div>
</body>
</html>
總結(jié):本例中使用了css3 的transform屬性中的 rotate的旋轉(zhuǎn)效果和translate的位移效果。
以上所述就是本文的全部?jī)?nèi)容了,希望本文能夠?qū)Υ蠹覍W(xué)習(xí)CSS3有所幫助。
相關(guān)文章
js+css3實(shí)現(xiàn)扁平化APP圖標(biāo)時(shí)鐘動(dòng)畫(huà)效果源碼
這是一款基于js+css3實(shí)現(xiàn)的扁平化APP圖標(biāo)時(shí)鐘動(dòng)畫(huà)效果源碼,是一款扁平化風(fēng)格APP時(shí)鐘動(dòng)態(tài)圖標(biāo)代碼,可事實(shí)顯示時(shí)鐘的時(shí)針、分針與秒針的運(yùn)行狀態(tài),畫(huà)面運(yùn)行效果流暢自然,2015-04-08一款很酷的CSS3翻頁(yè)時(shí)鐘動(dòng)畫(huà)特效源碼
今天我們來(lái)分享一款風(fēng)格獨(dú)特的時(shí)鐘特效源碼,這款CSS3時(shí)鐘動(dòng)畫(huà)是一款數(shù)字時(shí)鐘,時(shí)間每過(guò)一秒,相應(yīng)的位置就會(huì)像翻日歷一樣翻過(guò)去。另外,這款CSS3數(shù)字時(shí)鐘有點(diǎn)立體的效果,2014-11-22jquery+CSS3實(shí)現(xiàn)的數(shù)字時(shí)鐘效果源碼
這是一款基于jquery+CSS3實(shí)現(xiàn)的數(shù)字時(shí)鐘效果源碼,可實(shí)現(xiàn)年月日及具體時(shí)間的顯示,還可以讀取并實(shí)時(shí)顯示本地時(shí)間。并且在秒數(shù)的顯示上有閃動(dòng)的效果2014-10-20基于jQuery/CSS3實(shí)現(xiàn)的線(xiàn)性時(shí)鐘插件源碼
這是一款jQuery時(shí)鐘插件,它非常有特點(diǎn),即不是數(shù)字時(shí)鐘,也不是圓盤(pán)時(shí)鐘,而是基于線(xiàn)性的時(shí)鐘。其制作思想是把圓形的時(shí)鐘刻度展開(kāi)成直線(xiàn),把時(shí)針?lè)轴樅兔脶樢沧兂煽梢曰瑒?dòng)2014-07-01CSS3實(shí)現(xiàn)的圓盤(pán)時(shí)鐘效果動(dòng)畫(huà)源碼
這是一款超酷的純CSS3實(shí)現(xiàn)的圓盤(pán)時(shí)鐘動(dòng)畫(huà)效果,這款時(shí)鐘動(dòng)畫(huà)在初始化的時(shí)候就有動(dòng)畫(huà)特效,包括圓盤(pán)的形成,以及時(shí)鐘指針的形成,都賦予了極為炫酷的動(dòng)畫(huà)色彩。2014-06-20jQuery+CSS3實(shí)現(xiàn)的一款超酷數(shù)字時(shí)鐘翻頁(yè)動(dòng)畫(huà)
一款數(shù)字時(shí)鐘,時(shí)間每過(guò)一秒,相應(yīng)的位置就會(huì)像翻日歷一樣翻過(guò)去2014-05-04CSS3制作360度旋轉(zhuǎn)時(shí)鐘表不用任何圖片
一款完全利用CSS3實(shí)現(xiàn)鐘表,不是用任何的圖片和第三方的工具2014-04-14基于HTML5+CSS3實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘效果
利用html5,css實(shí)現(xiàn)鐘擺效果 ,在項(xiàng)目中經(jīng)常會(huì)遇到,今天小編把基于HTML5+CSS3實(shí)現(xiàn)簡(jiǎn)單的時(shí)鐘效果的實(shí)現(xiàn)代碼分享到腳本之家平臺(tái),需要的額朋友參考下吧2017-09-11