html酷炫电子时钟效果,纯js实现电子时钟特效

这是一款使用纯js实现电子时钟特效。该电子时钟可以显示时间,星期几,以及是上午还是下午。它使用简单,非常容易集成到项目中。

使用方法

在页面中引入电子时钟的样式文件main.css。

HTML结构

该电子时钟的基本HTML结构如下:

星期日

星期一

星期二

星期三

星期四

星期五

星期六

88

:

88

:

88

am

pm

JavaScript

该电子时钟通过下面的js代码来获取系统时间,并通过定时器来动态更新时间的显示。

window.onload = function(){

time();

ampm();

whatDay();

setInterval(function(){

time();

ampm();

whatDay();

}, 1000);

};

//gets current time and changes html to reflect it

function time(){

var date = new Date(),

hours = date.getHours(),

minutes = date.getMinutes(),

seconds = date.getSeconds();

//make clock a 12 hour clock instead of 24 hour clock

hours = (hours > 12) ? (hours - 12) : hours;

hours = (hours === 0) ? 12 : hours;

//invokes function to make sure number has at least two digits

hours = addZero(hours);

minutes = addZero(minutes);

seconds = addZero(seconds);

//changes the html to match results

document.getElementsByClassName('hours')[0].innerHTML = hours;

document.getElementsByClassName('minutes')[0].innerHTML = minutes;

document.getElementsByClassName('seconds')[0].innerHTML = seconds;

}

//turns single digit numbers to two digit numbers by placing a zero in front

function addZero (val){

return (val <= 9) ? ("0" + val) : val;

}

//lights up either am or pm on clock

function ampm(){

var date = new Date(),

hours = date.getHours(),

am = document.getElementsByClassName("am")[0].classList,

pm = document.getElementsByClassName("pm")[0].classList;

(hours >= 12) ? pm.add("light-on") : am.add("light-on");

(hours >= 12) ? am.remove("light-on") : pm.remove("light-on");

}

//lights up what day of the week it is

function whatDay(){

var date = new Date(),

currentDay = date.getDay(),

days = document.getElementsByClassName("day");

//iterates through all divs with a class of "day"

for (x in days){

//list of classes in current div

var classArr = days[x].classList;

(classArr !== undefined) && ((x == currentDay) ? classArr.add("light-on") : classArr.remove("light-on"));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值