`

“循环赛日程安排”问题的分而治之解决算法

 
阅读更多
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<title>UntitledPage</title>
<scripttype="text/javascript"language="javascript">
//为Number增加一个属性,判断当前数据类型是否是数字
Number.prototype.NaN0=function(){returnisNaN(this)?0:this;}

//全局变量
variMouseDown=false;
vardragObject=null;

//获得鼠标的偏移量(对象2-对象1)
functiongetMouseOffset(target,ev)
{
ev
=ev||window.event;
vardocPos=getPosition(target);
varmousePos=mouseCoords(ev);
return{x:mousePos.x-docPos.x,y:mousePos.y-docPos.y};
}


//获得事件发生的实际位置----------------------对象1
functiongetPosition(e)
{
varleft=0;
vartop=0;
//相对位置累加得到实际位置
while(e.offsetParent)
{
left
+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top
+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
e
=e.offsetParent;
}

left
+=e.offsetLeft+(e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
top
+=e.offsetTop+(e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
return{x:left,y:top};
}

//获得发生事件鼠标的位置----------------------对象2
functionmouseCoords(ev)
{
if(ev.pageX||ev.pageY)
{
return{x:ev.pageX,y:ev.pageY};
}

return{x:ev.clientX+document.body.scrollLeft-document.body.clientLeft,y:ev.clientY+document.body.scrollTop-document.body.clientTop};
}



//定义可以拖拽的对象
functionmakeDragable(item)
{
if(!item)return;
//为可拖拽对象定义一个onmousedown事件的方法
ev=window.event;
item.onmousedown
=function(ev)
{
dragObject
=this;
mouseOffset
=getMouseOffset(this,ev);
returnfalse;
}

}


//定义鼠标点下所调用的方法
functionmouseDown(ev)
{
ev
=ev||window.event;

vartarget=ev.target||ev.srcElement;
if(target.onmousedown||target.getAttribute('DragObj'))
{
returnfalse;
}

}

//鼠标抬起后释放对象
functionmouseUp(ev)
{
dragObject
=null;
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
iMouseDown=false;
}


//鼠标移动
functionmouseMove(ev)
{
ev
=ev||window.event;
vartarget=ev.target||ev.srcElement;
varmousePos=mouseCoords(ev);
if(dragObject)
{
if(dragObject.style)
{
//移动对象
dragObject.style.left=mousePos.x-mouseOffset.x;
dragObject.style.top
=mousePos.y-mouseOffset.y;
}

}

//lMouseState=iMouseDown;
if(dragObject)returnfalse;
}

document.onmousedown
=mouseDown;
document.onmousemove
=mouseMove;
document.onmouseup
=mouseUp;


functionmoveImg()
{
varimg1=document.getElementById('img1');
makeDragable(img1);
}

</script>
</head>
<bodyonload="moveImg()">
<div>
<imgsrc="http://images.163.com/homepage/logo.gif"alt=""id="img1"style="position:absolute;left:0px;top:0px;"/>
</div>
</body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics