照实例做,图片仍然无法跟随鼠标移动!请教
<head>
<title></title>
<script type="text/javascript">
document.onmousemove = function () {
var x = window.event.clientX;
var y = window.event.clientY;
var div1 = document.getElementById("div1");
if (!div1) {
return;
}
div1.style.left = x;
div1.style.top = y;
};
</script>
</head>
<body>
<div id="div1" style="position:absolute">
<img src="img\fly.png" /><br />
我是图片。
</div>
</body>
目的是要实现图片跟随鼠标移动,执行的时候鼠标稍微能往左边和上面动一下,然后就不动了,请教各位问题出在哪里了??谢谢
------解决方案--------------------我的IE9崩了懒得重装,你试试吧。
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
document.onmousemove = function (event) {
event= event
------解决方案-------------------- window.event
var x = (event.pageX != null) ? event.pageX - window.pageXOffset : event.clientX;
var y = (event.pageY != null) ? event.pageY - window.pageYOffset : event.clientY;
var div1 = document.getElementById("div1");
if (!div1) {
return;
}
div1.style.left = x;
div1.style.top = y;
};
</script>
</head>
<body>
<div id="div1" style="position:absolute">
<img src="img\fly.png" /><br />
我是图片。
</div>
</body>