js 随机数抓蚊子

来源:互联网 发布:阿里云短信群发接口 编辑:程序博客网 时间:2024/05/16 12:10
<script type="text/javascript">
window.onload = function(){
// 抓取元素
var img = document.getElementById("img");
// 获得可视区域的宽度和高度
var width = document.documentElement.clientWidth;
var height = document.documentElement.clientHeight;

// 添加鼠标移入事件
img.onmouseover = function(){
// 获得top值的随机数
var top = Math.floor(Math.random()*(height-100-0+1)+0);
// 获得left值的随机数
var left = Math.floor(Math.random()*(width-100-0+1)+0);

// 修改top值和left值

img.style.top = top+'px';
img.style.left = left+'px';
}
}

</script>

///////////////html/////////

<img id="img" src="img/wenzi.jpg" style="top: 0px; left: 0px;"/>

////////css///////////

<style type="text/css">
#img{
position: absolute;
width: 100px;
}
</style>

原创粉丝点击