javascript实现鼠标划过出现舞动的图片

   下面是我写的一个用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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用JavaScript实现鼠标悬浮改变图片位置的效果,具体实现步骤如下: 1. 获取需要操作的图片元素和容器元素。 2. 添加鼠标悬浮事件监听器,当鼠标悬浮在容器元素上时,获取鼠标在容器内的位置。 3. 计算图片应该移动的距离,并设置图片的样式`transform: translate(x, y)`来实现移动效果。 以下是示例代码: ```html <style> .container { position: relative; width: 500px; height: 300px; border: 1px solid #333; overflow: hidden; } img { position: absolute; top: 0; left: 0; transition: transform 0.3s ease-out; } </style> <div class="container" onmousemove="moveImage(event)"> <img src="example.jpg" alt="example" /> </div> <script> const container = document.querySelector('.container'); const image = document.querySelector('img'); function moveImage(event) { const containerRect = container.getBoundingClientRect(); const mouseX = event.clientX - containerRect.left; const mouseY = event.clientY - containerRect.top; const centerX = containerRect.width / 2; const centerY = containerRect.height / 2; const moveX = (mouseX - centerX) / 10; const moveY = (mouseY - centerY) / 10; image.style.transform = `translate(${moveX}px, ${moveY}px)`; } </script> ``` 该示例代码中,将图片放在一个容器中,容器的宽高和边框用于限制图片移动的范围。当鼠标在容器内移动时,计算鼠标相对于容器中心的偏移量,使用该偏移量计算图片应该移动的距离,并使用`transform`样式来实现图片移动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值