主页 个人前端笔记 div随鼠标任意移动
admin
发表于2012-11-11 18:55:22    只看楼主 楼主
头衔:  精益求精
注册时间: 2012-10-07
用户组: 日常管理组
发帖数:  502
金币数:  186
短消息
function(event){
var evt = window.event || event
traceX = evt.clientX,
traceY = evt.clientY,
objX = parseInt($('source_div').style.left || $('source_div').offsetLeft) || 0,
objY = parseInt($('source_div').style.top || $('source_div').offsetTop) || 0,
distance = Math.sqrt(Math.pow(traceX-objX ,2)+Math.pow(traceY-objY,2)),
//sin
s = (traceY-objY)/distance,
//cos
c = (traceX-objX)/distance,
t = 0;
clearInterval(st);
var move = function(){
t += 1;
if(t > distance)
{
t=distance, clearInterval(st);
}
$('source_div').style.left = objX+Math.round(t*c)+'px';
$('source_div').style.top = objY+Math.round(t*s)+'px';
}
st = setInterval(move,1)
}

回复人
回复内容

Powered BY YouYaX
个人自主开发论坛,从2010年10月份开发至今!

操作管理