服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

中文的版用javascript实现超酷的“网页时钟”

只须将如下的javascript代码插入到你页面html的<head>区即可!

<script language=javascript>
<!--
dcol='yellow'; //定义日历颜色
fcol='#ff0000'; //定义1-12这12个数的颜色
scol='#00ff00'; //定义秒针颜色
mcol='#0000ff'; //定义分针颜色
hcol='#ff0000'; //定义时针颜色
clockheight=40; //定义时钟的高度
clockwidth=40; //定义时钟的宽度
clockfrommousey=0; //定义时钟的中心距鼠标的相对垂直距离
clockfrommousex=100; //定义时钟中心距鼠标的相对水平距离
//以上颜色值你要据你页面的背景颜色进行修改!注意不要你页面的背景色一致哦。
d=new array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
m=new array("一月","二月","三月","四月","五月","六月","七月","八月","九月",
"十月","十一月","十二月"); //以上是给定星期和月份的取值范围
date=new date();
day=date.getdate(); //取得当前日期命令
year=date.getyear(); //取得当前年份命令
if (year < 2000) year=year+1900;
todaysdate="年 "+m[date.getmonth()]+" "+day+"日 "+d[date.getday()]+" "+year;
d=todaysdate.split(''); //显示"某年某月某日"
h='...';
h=h.split('');
m='....';
m=m.split('');
s='.....';
s=s.split(''); //使秒、分、时针反向相应的位置
face='1 2 3 4 5 6 7 8 9 10 11 12';
font='arial';
size=1; //定义秒、分、时针及1-12等字符的宋体、大小(最好匆改动哦)
speed=0.8; //定义一旦鼠标位置发生变化时所有相关字符跟随至前面指定的鼠标的相对位置的速度,
能看到各字符的轨迹,值可在0.1-1.0之间改动(值最小为0.1时跟随过来的速度最慢,值为1.0时跟随速度最快且与轨迹显示)
ns=(document.layers);
ie=(document.all); //说明在ns和ie两种不同浏览器里都适用

//以下的大段语句定义了ns和ie浏览器各自如何控制并完成时间和旋转的日历跟随鼠标转的
face=face.split(' ');
n=face.length;
a=size*10;
ymouse=0;
xmouse=0;
scrll=0;
props="<font face="+font+" size="+size+" color="+fcol+">";
props2="<font face="+font+" size="+size+" color="+dcol+">";
split=360/n;
dsplit=360/d.length;
handheight=clockheight/4.5
handwidth=clockwidth/4.5
handy=-7;
handx=-2.5;
scrll=0;
step=0.06;
currstep=0;
y=new array();x=new array();y=new array();x=new array();
for (i=0; i < n; i++){y[i]=0;x[i]=0;y[i]=0;x[i]=0}
dy=new array();dx=new array();dy=new array();dx=new array();
for (i=0; i < d.length; i++){dy[i]=0;dx[i]=0;dy[i]=0;dx[i]=0}

if (ns){
for (i=0; i < d.length; i++)
document.write('<layer name="nsdate'+i+'" top=0 left=0 height='+a+' width='+a+'><center>'+props2+d[i]+'</font></center></layer>');
for (i=0; i < n; i++)
document.write('<layer name="nsface'+i+'" top=0 left=0 height='+a+' width='+a+'><center>'+props+face[i]+'</font></center></layer>');
for (i=0; i < s.length; i++)
document.write('<layer name=nsseconds'+i+' top=0 left=0 width=15
height=15><font face=arial size=3 color='+scol+'><center><b>'+s[i]+'</b></center></font></layer>');
for (i=0; i < m.length; i++)
document.write('<layer name=nsminutes'+i+' top=0 left=0 width=15
height=15><font face=arial size=3 color='+mcol+'><center><b>'+m[i]+'</b></center></font></layer>');
for (i=0; i < h.length; i++)
document.write('<layer name=nshours'+i+' top=0 left=0 width=15
height=15><font face=arial size=3 color='+hcol+'><center><b>'+h[i]+'</b></center></font></layer>');
}

if (ie){
document.write('<div id="od" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < d.length; i++)
document.write('<div id="iedate" style="position:absolute;top:0px;left:0;height:'+a+';width:'+a+';text-align:center">'+props2+d[i]+'</font></div>');
document.write('</div></div>');
document.write('<div id="of" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < n; i++)
document.write('<div id="ieface" style="position:absolute;top:0px;left:0;height:'+a+';width:'+a+';text-align:center">'+props+face[i]+'</font></div>');
document.write('</div></div>');
document.write('<div id="oh" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < h.length; i++)
document.write('<div id="iehours" style="position:absolute;width:16px;
height:16px;font-family:arial;font-size:16px;color:'+hcol+';text-align:center;font-weight:bold">'+h[i]+'</div>');
document.write('</div></div>');
document.write('<div id="om" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < m.length; i++)
document.write('<div id="ieminutes" style="position:absolute;width:16px;
height:16px;font-family:arial;font-size:16px;color:'+mcol+';text-align:center;font-weight:bold">'+m[i]+'</div>');
document.write('</div></div>')
document.write('<div id="os" style="position:absolute;top:0px;left:0px"><div style="position:relative">');
for (i=0; i < s.length; i++)
document.write('<div id="ieseconds" style="position:absolute;width:16px;
height:16px;font-family:arial;font-size:16px;color:'+scol+';text-align:center;font-weight:bold">'+s[i]+'</div>');
document.write('</div></div>')
}
(ns)?window.captureevents(event.mousemove):0;
function mouse(evnt){
ymouse = (ns)?evnt.pagey+clockfrommousey-(window.pageyoffset):
event.y+clockfrommousey;
xmouse = (ns)?evnt.pagex+clockfrommousex:event.x+clockfrommousex;
}
(ns)?window.onmousemove=mouse:document.onmousemove=mouse;
function clockandassign(){
time = new date ();
secs = time.getseconds();
sec = -1.57 + math.pi * secs/30;
mins = time.getminutes();
min = -1.57 + math.pi * mins/30;
hr = time.gethours();
hrs = -1.575 + math.pi * hr/6+math.pi*parseint(time.getminutes())/360;
if (ie){
od.style.top=window.document.body.scrolltop;
of.style.top=window.document.body.scrolltop;
oh.style.top=window.document.body.scrolltop;
om.style.top=window.document.body.scrolltop;
os.style.top=window.document.body.scrolltop;
}
for (i=0; i < n; i++){
var f=(ns)?document.layers['nsface'+i]:ieface[i].style;
f.top=y[i] + clockheight*math.sin(-1.0471 + i*split*math.pi/180)+scrll;
f.left=x[i] + clockwidth*math.cos(-1.0471 + i*split*math.pi/180);
}
for (i=0; i < h.length; i++){
var hl=(ns)?document.layers['nshours'+i]:iehours[i].style;
hl.top=y[i]+handy+(i*handheight)*math.sin(hrs)+scrll;
hl.left=x[i]+handx+(i*handwidth)*math.cos(hrs);
}
for (i=0; i < m.length; i++){
var ml=(ns)?document.layers['nsminutes'+i]:ieminutes[i].style;
ml.top=y[i]+handy+(i*handheight)*math.sin(min)+scrll;
ml.left=x[i]+handx+(i*handwidth)*math.cos(min);
}
for (i=0; i < s.length; i++){
var sl=(ns)?document.layers['nsseconds'+i]:ieseconds[i].style;
sl.top=y[i]+handy+(i*handheight)*math.sin(sec)+scrll;
sl.left=x[i]+handx+(i*handwidth)*math.cos(sec);
}
for (i=0; i < d.length; i++){
var dl=(ns)?document.layers['nsdate'+i]:iedate[i].style;
dl.top=dy[i] + clockheight*1.5*math.sin(currstep+i*dsplit*math.pi/180)+scrll;
dl.left=dx[i] + clockwidth*1.5*math.cos(currstep+i*dsplit*math.pi/180);
}
currstep-=step;
}
function delay(){
scrll=(ns)?window.pageyoffset:0;
dy[0]=math.round(dy[0]+=((ymouse)-dy[0])*speed);
dx[0]=math.round(dx[0]+=((xmouse)-dx[0])*speed);
for (i=1; i < d.length; i++){
dy[i]=math.round(dy[i]+=(dy[i-1]-dy[i])*speed);
dx[i]=math.round(dx[i]+=(dx[i-1]-dx[i])*speed);
}
y[0]=math.round(y[0]+=((ymouse)-y[0])*speed);
x[0]=math.round(x[0]+=((xmouse)-x[0])*speed);
for (i=1; i < n; i++){
y[i]=math.round(y[i]+=(y[i-1]-y[i])*speed);
x[i]=math.round(x[i]+=(x[i-1]-x[i])*speed);
}
clockandassign();
settimeout('delay()',20);
}
if (ns||ie)window.onload=delay;
//-->
</script>

扫描关注微信公众号