本網(wǎng)頁(yè)展示了利用javascript技術(shù)利用JS技術(shù)實(shí)現(xiàn)鼠標(biāo)滑過(guò)圖片時(shí)震動(dòng)的效果,也許各位在日常的網(wǎng)站建設(shè)中可能能夠用到,尤其是在實(shí)現(xiàn)一些特效的時(shí)候。本例中有幾個(gè)參數(shù)需要說(shuō)明一下:rector表示震動(dòng)幅度,值越大震動(dòng)幅度越大,您可以修改此值來(lái)控制圖片震動(dòng)的幅度; stopit用于控制震動(dòng)狀態(tài),值為0表示可以震動(dòng)了,而在震動(dòng)狀態(tài)中如果值改變?yōu)?,則停止震動(dòng)。下面,鄭州網(wǎng)站建設(shè)公司的小編整理了此功能,本代碼已經(jīng)在IE下測(cè)試通過(guò),可供建站用戶和網(wǎng)站建設(shè)技術(shù)人員參考。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>利用JS技術(shù)實(shí)現(xiàn)鼠標(biāo)滑過(guò)圖片時(shí)震動(dòng)的效果</title>
<STYLE type="text/css">
.showimage {POSITION: relative;
}
</STYLE>
<SCRIPT language="javascript">
<!--
var rector=3 //震動(dòng)幅度
var stopit=0 //是否停止震動(dòng),0--震動(dòng),1--停止
var image; //圖片對(duì)象
var loop=1; //循環(huán)控制變量
//初始化可用變量
function init(img)
{
stopit=0
image=img;
image.style.left=0
image.style.top=0
}
//實(shí)現(xiàn)震動(dòng)圖片
function zhendong_image()
{
if ((!document.all&&!document.getElementById)||stopit==1)
return;
if (loop==1){
image.style.top=parseInt(image.style.top)+rector
}
else if (loop==2){
image.style.left=parseInt(image.style.left)+rector
}
else if (loop==3){
image.style.top=parseInt(image.style.top)-rector
}
else{
image.style.left=parseInt(image.style.left)-rector
}
if (loop<4)
loop++;
else
loop=1;
setTimeout("zhendong_image()",50)
}
//鼠標(biāo)離開(kāi)時(shí)執(zhí)行該函數(shù)功能,恢復(fù)初始值
function stop(image)
{
stopit=1; //停止震動(dòng)
image.style.left=0; //恢復(fù)初始值
image.style.top=0; //恢復(fù)初始值
}
//-->
</SCRIPT>
</head>
<body>
<img class="showimage" onMouseOver="init(this); zhendong_image();" onMouseOut="stop(this)" src="http://m.511sj.com/images/logo.jpg" border="0" style="cursor:pointer;" />
</body>
</html>