<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
<script type="text/javascript">
document.onmousemove=picfloat;
function inicial() {
document.getElementById("pic").style.left="0px";
document.getElementById("pic").style.top="0px";
}
var left, top;
var speed=5;
function picfloat(event) {
var event=window.event||event;
while(1) {
left=document.getElementById("pic").style.left;
top=document.getElementById("pic").style.top;
t=setTimeout(follow(left,top),1);
}
}
function follow(left,top) {
if(parseInt(left)<=parseInt(event.clientX)) {
document.getElementById("pic").style.left=(parseInt(left)+speed)+"px";
} else {
document.getElementById("pic").style.left=(parseInt(left)-speed)+"px";
}
if(parseInt(top)<=parseInt(event.clientY)) {
document.getElementById("pic").style.top=(parseInt(top)+speed)+"px";
} else {
document.getElementById("pic").style.top=(parseInt(top)-speed)+"px";
}
}
</script>
<style type="text/css">
#pic {
position:absolute;
}
</style>
</head><body onload="inicial()"><div>
<img src="images/xmsx.gif" onmouseover="document.location.href='Apr1st.html'" id="pic"/>
</div> </body>
</html>
这段代码 当鼠标不移动的时候 图片移动的就非常慢 应该如何解决呢
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>无标题文档</title>
<script type="text/javascript">
document.onmousemove=picfloat;
function inicial() {
document.getElementById("pic").style.left="0px";
document.getElementById("pic").style.top="0px";
}
var left, top;
var speed=5;
function picfloat(event) {
var event=window.event||event;
while(1) {
left=document.getElementById("pic").style.left;
top=document.getElementById("pic").style.top;
t=setTimeout(follow(left,top),1);
}
}
function follow(left,top) {
if(parseInt(left)<=parseInt(event.clientX)) {
document.getElementById("pic").style.left=(parseInt(left)+speed)+"px";
} else {
document.getElementById("pic").style.left=(parseInt(left)-speed)+"px";
}
if(parseInt(top)<=parseInt(event.clientY)) {
document.getElementById("pic").style.top=(parseInt(top)+speed)+"px";
} else {
document.getElementById("pic").style.top=(parseInt(top)-speed)+"px";
}
}
</script>
<style type="text/css">
#pic {
position:absolute;
}
</style>
</head><body onload="inicial()"><div>
<img src="images/xmsx.gif" onmouseover="document.location.href='Apr1st.html'" id="pic"/>
</div> </body>
</html>
这段代码 当鼠标不移动的时候 图片移动的就非常慢 应该如何解决呢
document.onmousemove=picfloat;并且判断了鼠标的位置
if(parseInt(left)<=parseInt(event.clientX)) 如果鼠标不动,应该不移动了