图片跟随鼠标移动的问题

pygnzj 2012-03-31 06:47:10
<!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>


这段代码 当鼠标不移动的时候 图片移动的就非常慢 应该如何解决呢
...全文
149 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pygnzj 2012-03-31
  • 打赏
  • 举报
回复
在本地测试没问题 发布到IIS上就不动了是怎么回事呢
wzhiyuan 2012-03-31
  • 打赏
  • 举报
回复
<img style="position:absolute;" src="http://c.csdn.net/bbs/t/5/i/pic_logo.gif" id="img1" />
[code=Jscript]
window.setInterval("move()", 100);
var left = 0;
var top = 0;
function move() {
document.getElementById("img1").style.left = left + "px";
document.getElementById("img1").style.top = top + "px";
left++;
top++;
}
[/code]
pygnzj 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

while(1) {
left=document.getElementById("pic").style.left;
top=document.getElementById("pic").style.top;
t=setTimeout(follow(left,top),1);
}
这种写法不慢才怪

你设置一个setTimeout就行了,没必要while的
[/Quote]
可是不用while鼠标不动 图片也不动了啊
wzhiyuan 2012-03-31
  • 打赏
  • 举报
回复
while(1) {
left=document.getElementById("pic").style.left;
top=document.getElementById("pic").style.top;
t=setTimeout(follow(left,top),1);
}
这种写法不慢才怪

你设置一个setTimeout就行了,没必要while的

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧