网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

时钟javascript的简单介绍

JavaScript 如何实现的简单时钟?

背景时钟

成都创新互联公司专业提供成都电信服务器托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买成都电信服务器托管服务,并享受7*24小时金牌售后服务。

====1、以下是这个效果的全部代码。[最好从一个空页面开始]

html

head

TITLE背景时钟/TITLE

script language=javaScript

!--//

function clockon() {

thistime= new Date()

var hours=thistime.getHours()

var minutes=thistime.getMinutes()

var seconds=thistime.getSeconds()

if (eval(hours) 10) {hours="0"+hours}

if (eval(minutes) 10) {minutes="0"+minutes}

if (seconds 10) {seconds="0"+seconds}

thistime = hours+":"+minutes+":"+seconds

if(document.all) {

bgclocknoshade.innerHTML=thistime

bgclockshade.innerHTML=thistime

}

if(document.layers) {

document.bgclockshade.document.write('div id="bgclockshade" style="position:absolute;visibility:visible;font-family:Verdana;color:FFAAAAA;font-size:120px;top:10px;left:152px"'+thistime+'/div')

document.bgclocknoshade.document.write('div id="bgclocknoshade" style="position:absolute;visibility:visible;font-family:Verdana;color:DDDDDD;font-size:120px;top:10px;left:150px"'+thistime+'/div')

document.close()

}

var timer=setTimeout("clockon()",200)

}

//--

/script

link rel="stylesheet" href="../style.css"/head

body onLoad="clockon()"

div id="bgclockshade" style="position:absolute;visibility:visible;font-family:Arial;color:FF8888;font-size:120px;top:102px;left:152px"/div

div id="bgclocknoshade" style="position:absolute;visibility:visible;font-family:Arial;color:DDDDDD;font-size:120px;top:100px;left:150px"/div

div id="mainbody" style="position:absolute; visibility:visible"

/div

/body

/html

说明:时钟显示的位置需要你修正TOP,LEFT参数来确定。TOP表示层距离显示器顶部的象素值,LEFT表示距离左侧的象素。

[img]

如何使用JS实现一个简易数码时钟

设计思路:

数码时钟即通过图片数字来显示当前时间,需要显示的图片的URL根据时间变化而变化。

a、获取当前时间Date()并将当前时间信息转换为一个6位的字符串;

b、根据时间字符串每个位置对应的数字来更改图片的src的值,从而实现更换显示图片;

构建HTML基础并添加样式。

div id="div1"

img src='./数字时钟(1)_files/0.jpg'

img src='./数字时钟(1)_files/0.jpg'

:

img src='./数字时钟(1)_files/0.jpg'

img src='./数字时钟(1)_files/0.jpg'

:

img src='./数字时钟(1)_files/0.jpg'

img src='./数字时钟(1)_files/0.jpg'

/div

style样式

#div1{

width:50%;

margin:300px auto;

background:black;

}

获取图片元素以及当前时间:

var oDiv=document.getElementById('div1');

var aImg=oDiv.getElementsByTagName('img');

var oDate=new Date();

var str=oDate.getHours()+oDate.getMinutes()+oDate.getSeconds();

这里出现两个问题

1、oDate.getHours()返回的都是数字,这样编写为数字相加,而非字符串相加。

2、当获取的值为一位数时,会造成字符串的个数少于6,不满足初始设计要求。

解决以上两个问题的代码解决方案:

代码

var oDiv=document.getElementById('div1');

var aImg=oDiv.getElementsByTagName('img');

var oDate=new Date();

function twoDigitsStr()

{

if(n10)

{return '0'+n;}

else

{return ''+n;}

}

var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds());

设置所有图片的src值:

for(var i=0;iaImg.length;i++)

{

aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg";

}

通过setInterval()来实现每隔1秒进行重新获取当前时间以及图片src值:

代码

var oDiv=document.getElementById('div1');

var aImg=oDiv.getElementsByTagName('img');

setInterval(function tick()

{

var oDate=new Date();

var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds());

for(var i=0;iaImg.length;i++)

{

aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg";

}

}

,1000);

但是还是有一个问题,网页在打开的初始阶段,显示时间为00:00:00,这是因为定时器有个特性,当定时器被打开后,不会立刻执行里面的函数,而是在1秒后执行。解决代码:

var oDiv=document.getElementById('div1');

var aImg=oDiv.getElementsByTagName('img');

function tick()

{var oDate=new Date();

var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds());

for(var i=0;iaImg.length;i++)

{

aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg";

}

}

setInterval(tick,1000);

tick();

问题:代码setInterval(tick,1000);中函数tick没有带括号,那么JS中函数带括号与不带括号有什么区别?

完整的数码时钟制作JS代码为:

function twoDigitsStr(n)

{

if(n10)

{return '0'+n;}

else

{return ''+n;}

}

window.onload=function()

{

var oDiv=document.getElementById('div1');

var aImg=oDiv.getElementsByTagName('img');

function tick()

{var oDate=new Date();

var str=twoDigitsStr(oDate.getHours())+twoDigitsStr(oDate.getMinutes())+twoDigitsStr(oDate.getSeconds());

for(var i=0;iaImg.length;i++)

{

aImg[i].src="./数字时钟(1)_files/"+str.charAt(i)+".jpg";

}

}

setInterval(tick,1000);

tick(); 

}

如何用javascript实现一个时钟?

function init(){

clock();

setInterval(clock,1000);

}

function clock(){

var now = new Date();

var ctx = document.getElementById('canvas').getContext('2d');

ctx.save();

ctx.clearRect(0,0,150,150);

ctx.translate(75,75);

ctx.scale(0.4,0.4);

ctx.rotate(-Math.PI/2);

ctx.strokeStyle = "black";

ctx.fillStyle = "white";

ctx.lineWidth = 8;

ctx.lineCap = "round";

// Hour marks

ctx.save();

for (var i=0;i12;i++){

ctx.beginPath();

ctx.rotate(Math.PI/6);

ctx.moveTo(100,0);

ctx.lineTo(120,0);

ctx.stroke();

}

ctx.restore();

// Minute marks

ctx.save();

ctx.lineWidth = 5;

for (i=0;i60;i++){

if (i%5!=0) {

ctx.beginPath();

ctx.moveTo(117,0);

ctx.lineTo(120,0);

ctx.stroke();

}

ctx.rotate(Math.PI/30);

}

ctx.restore();

var sec = now.getSeconds();

var min = now.getMinutes();

var hr  = now.getHours();

hr = hr=12 ? hr-12 : hr;

ctx.fillStyle = "black";

// write Hours

ctx.save();

ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )

ctx.lineWidth = 14;

ctx.beginPath();

ctx.moveTo(-20,0);

ctx.lineTo(80,0);

ctx.stroke();

ctx.restore();

// write Minutes

ctx.save();

ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )

ctx.lineWidth = 10;

ctx.beginPath();

ctx.moveTo(-28,0);

ctx.lineTo(112,0);

ctx.stroke();

ctx.restore();

// Write seconds

ctx.save();

ctx.rotate(sec * Math.PI/30);

ctx.strokeStyle = "#D40000";

ctx.fillStyle = "#D40000";

ctx.lineWidth = 6;

ctx.beginPath();

ctx.moveTo(-30,0);

ctx.lineTo(83,0);

ctx.stroke();

ctx.beginPath();

ctx.arc(0,0,10,0,Math.PI*2,true);

ctx.fill();

ctx.beginPath();

ctx.arc(95,0,10,0,Math.PI*2,true);

ctx.stroke();

ctx.fillStyle = "#555";

ctx.arc(0,0,3,0,Math.PI*2,true);

ctx.fill();

ctx.restore();

ctx.beginPath();

ctx.lineWidth = 14;

ctx.strokeStyle = '#325FA2';

ctx.arc(0,0,142,0,Math.PI*2,true);

ctx.stroke();

ctx.restore();

}

javascript动态显示时间

html

head

title时钟特效/title

script type="text/javascript"

function disptime(){

var today = new Date(); //获得当前时间

var hh = today.getHours(); //获得小时、分钟、秒

var mm = today.getMinutes();

var ss = today.getSeconds();

/*设置div的内容为当前时间*/

document.getElementById("myclock").innerHTML="h1现在是:"+hh+":"+mm+":"+ss+"h1";

/*

使用setTimeout在函数disptime()体内再次调用setTimeout

设置定时器每隔1秒(1000毫秒),调用函数disptime()执行,刷新时钟显示

*/

var myTime=setTimeout("disptime()",1000);

}

/script

/head

body onload="disptime()"

div id="myclock"/div

/body。


新闻标题:时钟javascript的简单介绍
转载来于:http://bjjierui.cn/article/dsophsp.html

其他资讯