下面是我写的一个用javascript实现的鼠标点击或划过就会随机出现图片的效果,图片的位置是随机产生的,当你的鼠标滑动的够快的话,会看到更加神奇的效果,当然你的图片也要选择的好,我相信各位的眼光都会比我好,由于我未上传图片,因此在你复制代码后,你的同目录下的有图片才能查看效果哦,初次尝试,代码不够精简,日后会分享更好的东西。。。
ps:这是我看过这个Flash后由此而萌生的想法,即用javascript实现相同的效果,附上源Flash链接:http://www.procreo.jp/labo/flower_garden.swf
源代码:
<!DOCTYPE HTML>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>javascript实现鼠标划过出现舞动的图片</title>
<script type="text/javascript">
var st = 0;function f1(){st++;switch(st){case 1: document.getElementById("a1").style.display="block";document.getElementById("a1").style.top=fff(); //产生随机位置document.getElementById("a1").style.left=fff1(); break;case 2: document.getElementById("a2").style.display="block";document.getElementById("a2").style.top=fff();document.getElementById("a2").style.left=fff1(); break;case 3: document.getElementById("a3").style.display="block";document.getElementById("a3").style.top=fff();document.getElementById("a3").style.left=fff1(); break;case 4: document.getElementById("a4").style.display="block";document.getElementById("a4").style.top=fff();document.getElementById("a4").style.left=fff1(); break;case 5: document.getElementById("a5").style.display="block";document.getElementById("a5").style.top=fff();document.getElementById("a5").style.left=fff1(); break;case 6: document.getElementById("a6").style.display="block";document.getElementById("a6").style.top=fff();document.getElementById("a6").style.left=fff1();break;case 7: document.getElementById("a7").style.display="block";document.getElementById("a7").style.top=fff();document.getElementById("a7").style.left=fff1(); break;case 8: document.getElementById("a8").style.display="block";document.getElementById("a8").style.top=fff();document.getElementById("a8").style.left=fff1(); break;case 9:document.getElementById("a9").style.display="block";document.getElementById("a9").style.top=fff();document.getElementById("a9").style.left=fff1();break;case 10: document.getElementById("a10").style.display="block";document.getElementById("a10").style.top=fff();document.getElementById("a10").style.left=fff1();break;case 11: document.getElementById("a11").style.display="block";document.getElementById("a11").style.top=fff();document.getElementById("a11").style.left=fff1();break;case 12: document.getElementById("a12").style.display="block";document.getElementById("a12").style.top=fff();document.getElementById("a12").style.left=fff1();break;case 13: document.getElementById("a13").style.display="block";document.getElementById("a13").style.top=fff();document.getElementById("a13").style.left=fff1();break;case 14: document.getElementById("a14").style.display="block";document.getElementById("a14").style.top=fff();document.getElementById("a14").style.left=fff1();break;case 15: document.getElementById("a15").style.display="block";document.getElementById("a15").style.top=fff();document.getElementById("a15").style.left=fff1();break;case 16: document.getElementById("a16").style.display="block";document.getElementById("a16").style.top=fff();document.getElementById("a16").style.left=fff1();break;default:st=st%16;break;}
}function fff(){//设置页面中Y方向上的随机数var nn=(Math.random()*600);nn = String(nn)+ "px";return nn;}function fff1(){//设置页面中X方向上的随机数var nn1=(Math.random()*1200);nn1 = String(nn1)+ "px";return nn1;}</script><style>body{ background:#000;}#a1{position:absolute;
z-index:1;}#a2{position:absolute;
z-index:2;
}#a3{position:absolute;
z-index:3;
}#a4{position:absolute;
z-index:4;
}#a5{position:absolute;z-index:5;
}#a6{position:absolute;z-index:6;
}#a7{position:absolute;z-index:7;
}#a8{position:absolute;z-index:8;
}#a9{position:absolute;
z-index:9;
}#a10{position:absolute;
z-index:10;
}#a11{position:absolute;
z-index:11;
}#a12{position:absolute;
z-index:12;
}#a13{position:absolute;
z-index:13;
}#a14{position:absolute;
z-index:14;
}#a15{position:absolute;
z-index:15;
}#a16{position:absolute;
z-index:16;
}#b{width:1366px;height:768px;background:#000;
}</style></head>
<body>
<img src="黑色.jpg" usemap="#Map"/><map name="Map"><area shape="rect" coords="0,0,1366,768" οnclick="f1()" onMouseOver="f1()" value=""></map></br><img id="a1" src="1.png" width="100" height="100" style="display:none"/><img id="a2" src="2.png" width="100" height="100" style="display:none"/><img id="a3" src="3.png" width="100" height="100" style="display:none"/><img id="a4" src="4.png" width="100" height="100" style="display:none"/><img id="a5" src="5.png" width="100" height="100" style="display:none"/><img id="a6" src="6.png" width="100" height="100" style="display:none"/><img id="a7" src="7.png" width="100" height="100" style="display:none"/><img id="a8" src="8.png" width="100" height="100" style="display:none"/><img id="a9" src="9.png" width="100" height="100" style="display:none"/><img id="a10" src="10.png" width="100" height="100" style="display:none"/><img id="a11" src="11.png" width="100" height="100" style="display:none"/><img id="a12" src="12.png" width="100" height="100" style="display:none"/><img id="a13" src="13.png" width="100" height="100" style="display:none"/><img id="a14" src="14.png" width="100" height="100" style="display:none"/><img id="a15" src="15.png" width="100" height="100" style="display:none"/><img id="a16" src="16.png" width="100" height="100" style="display:none"/>
</body>
</html>