您的位置:首页 > 编程语言 > ASP

asp.net中鼠標移動圖片

2008-01-09 15:41 246 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<html>
<head>
<title>Test</title>
<script language="javascript">
var ox,oy,ex,ey;
var stepx,stepy,TimeSpan=20;
var ts=null;
document.onclick=Handler;
var MoveObj=null;
window.onload=function()
{
MoveObj=document.getElementById("pic");
}
function Handler(e)
{
clearInterval(ts);//
e=e||event;
alert(e);
var pic=document.getElementById("pic");
ox=parseInt(pic.style.left);
oy=parseInt(pic.style.top);
ex=e.clientX;
ey=e.clientY;
if(ex>ox)
{
stepx=1;
}
else
{
stepx=-1;
}
stepy=(ey-oy)/(ex-ox);//根据比例算出y方向的移动速度
ts=setInterval("movePic()",TimeSpan);
}
function movePic()
{
ox+=stepx;
oy+=stepy;
if(stepx>0)
{
if(ox<=ex)
MoveObj.style.left=ox+"px";
else
MoveObj.style.left=ex+"px";
}
else
{
if(ox>=ex)
MoveObj.style.left=ox+"px";
else
MoveObj.style.left=ex+"px";
}
if(stepy>0)
{
if(oy<=ey)
MoveObj.style.top=oy+"px";
else
MoveObj.style.top=ey+"px";
}
else
{
if(oy>=ey)
MoveObj.style.top=oy+"px";
else
MoveObj.style.top=ey+"px";
}
if(parseFloat(MoveObj.style.top)==parseInt(ey)&&parseFloat(MoveObj.style.left)==parseInt(ex))
clearInterval(ts);
}
</script>
</head>
<body>
<img src="images/toqq.gif" id="pic" style='position:absolute;left:0px;top:0px;width:120px;height:100px'/>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: