?? mouseclock.htm
字號:
<HTML>
<HEAD>
<TITLE>
與鼠標聯動的動畫時鐘
</TITLE>
<script language=JavaScript>
dCol='000099';//日期的顏色.
fCol='668099';//面板的顏色.
sCol='FF0000';//秒針的顏色.
mCol='000000';//分針的顏色.
hCol='000000';//時針的顏色.
ClockHeight=40;//時鐘的高度
ClockWidth=40;//時鐘的寬度
ClockFromMouseY=0;//在縱坐標方向上時鐘和鼠標之間的距離
ClockFromMouseX=100;//在橫坐標方向上時鐘和鼠標之間的距離
d=new Array("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SATURDAY");//星期數組
m=new Array("JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER");//月份數組
date=new Date();//獲得當前時間的一個實例
day=date.getDate();//獲得日期
year=date.getYear();//獲得年份
if(year < 2000) //若小于2000年,則該年份是兩位數,應該加上1900
year=year+1900;
TodaysDate=" "+d[date.getDay()]+" "+day+" "+m[date.getMonth()]+" "+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='Verdana';//設置字體
size=1;//用來計算各容器大小的參數
speed=0.6;//用來計算文字環繞的速度
ns=(document.layers);//判斷瀏覽器是Netscape
ie=(document.all);//判斷瀏覽器是IE
Face=Face.split(' ');//面板上的顯示的組成字符數組
n=Face.length;//上述數組的長度,實際上就是12
a=size*10;//設置各個容器的大小
ymouse=0;//時鐘的縱坐標
xmouse=0;//時鐘的橫坐標
scrll=0;//設置Netscape中頁面的縱坐標偏移
FaceFont="<font face="+font+" size="+size+" color="+fCol+"><B>";//設置面板的字體和顏色
DayFont="<font face="+font+" size="+size+" color="+dCol+"><B>";//設置日期的字體和顏色
Split=360/n;//兩個表示小時的數字中間分開的角度,實際上是30度。
Dsplit=360/D.length;//兩個D數組中的相鄰字符所分開的角度
HandHeight=ClockHeight/4.5 //各針的高度
HandWidth=ClockWidth/4.5 //各針的寬度
HandY=-7;//設置時針、分針、秒針的縱坐標修正
HandX=-2.5;//設置時針、分針、秒針的橫坐標修正
step=0.06;//用于累計計算currStep
currStep=0;//用于計算角度的參數
y=new Array();//下面Y數組的四舍五入值
x=new Array();//下面X數組的四舍五入值
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();//下面DY數組的四舍五入值
Dx=new Array();//下面DX數組的四舍五入值
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
}
//Netscape瀏覽器的情況,請參看下面IE瀏覽器的注釋
if(ns)
{
for(i=0; i < D.length;i++)
document.write('<layer name="nsDate'+i+'" top=0 left=0 height='+a+' width='+a+'><center>'+DayFont+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>'+FaceFont+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>');
}
//IE瀏覽器的情況
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">'+DayFont+D[i]+'</B></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">'+FaceFont+Face[i]+'</B></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>')
}
//Netscape和IE的MouseMove事件處理函數
(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;//根據鼠標的橫坐標位置設置時鐘的橫坐標
}
//設置Netscape或者IE的MouseMove事件處理函數
(ns)?window.onMouseMove=Mouse:document.onmousemove=Mouse;
//設置時鐘運行的函數
function ClockAndAssign()
{
time = new Date();//獲得當前時間的一個實例
secs = time.getSeconds();//獲得當前的秒數
sec = -1.57 + Math.PI * secs/30;//根據秒數來計算角度值,也就是秒針應處于的角度。當secs=60時,sec相當于2*PI,正好轉過一圈。
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賦值為面板的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;//step累計計算currStep
}
//動畫時鐘顯示的入口函數
function Delay()
{
scrll=(ns)?window.pageYOffset:0;//設置scrll的值,用來在Netscape瀏覽器中修正各容器的縱坐標
//下面的程序段用來計算各個日期字符的顯示位置
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()',10);//循環定時器
}
if(ns||ie)window.onload=Delay;//將onLoad事件的處理函數設為Delay函數
</script>
</HEAD>
<BODY>
</BODY>
</HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -