小编给大家分享一下如何使用CSS3+js实现简单的时钟特效,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!html代码如下:
css 代码如下:
2. 初始化默认时间,和表盘刻度 :更改后的css代码:
初始化 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;
//初始化表盘
for (var index = 0; index CreateKeDu(main, “hourPointer”, index * 90, 138);
}
for (var index = 0; index CreateKeDu(main, “minuterPointer”,index*30, 140);
}
for (var index = 0; index CreateKeDu(main, “secondPointer”, index * 6, 142);
}
//初始化时分秒针
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.添加定时器:js代码如下:复制代码代码如下:
//定时器
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;
//初始化函数
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;
//初始化表盘
for (var index = 0; index this免费云主机域名.CreateKeDu(“hourPointer”, index * 90, 138);
}
for (var index = 0; index this.CreateKeDu(“minuterPointer”, index * 30, 140);
}
for (var index = 0; 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();
}最终页面代码:复制代码代码如下:
http://www.w3.org/1999/xhtml”>
以上是“如何使用CSS3+js实现简单的时钟特效”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注百云主机行业资讯频道!
这篇文章主要介绍了实体门店小程序开发有什么优势呢,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。1。低成本开发从每个软件开发市场的角度来看,开发一组小型商店该程序的成本比开发应用程序的成本低得多。…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。