<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';
}
}
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>