<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#a1{
width: 100px;
height: 100px;
background: lawngreen;
position: absolute;
}
</style>
<script type="text/javascript" >
window.οnlοad=function(){
//keydouwn onmousemove keyup
var odiv=document.getElementById("a1");
var odivX=0;
var odivY=0;
odiv.οnmοusedοwn=function(ev){
var ev=ev||event;
odivX=ev.clientX-odiv.offsetLeft;
odivY=ev.clientY-odiv.offsetTop;
document.οnmοusemοve=function(ev){
var ev=ev||event;
var left=ev.clientX-odivX;
var top=ev.clientY-odivY;
if(left<0){
left=0;
}else if (left>document.documentElement.clientWidth-odiv.offsetWidth){
left=document.documentElement.clientWidth-odiv.offsetWidth
}
if(top<0){
top=0;
}else if (top>document.documentElement.clientHeight-odiv.offsetHeight){
top=document.documentElement.clientHeight-odiv.offsetHeight
}
odiv.style.left=left+"px";
odiv.style.top=top+"px";
}
}
document.οnmοuseup=function(){
document.οnmοusemοve=null;
odiv.οnmοuseup=null;
}
}
</script>
</head>
<body>
<div id="a1"></div>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#a1{
width: 100px;
height: 100px;
background: lawngreen;
position: absolute;
}
</style>
<script type="text/javascript" >
window.οnlοad=function(){
//keydouwn onmousemove keyup
var odiv=document.getElementById("a1");
var odivX=0;
var odivY=0;
odiv.οnmοusedοwn=function(ev){
var ev=ev||event;
odivX=ev.clientX-odiv.offsetLeft;
odivY=ev.clientY-odiv.offsetTop;
document.οnmοusemοve=function(ev){
var ev=ev||event;
var left=ev.clientX-odivX;
var top=ev.clientY-odivY;
if(left<0){
left=0;
}else if (left>document.documentElement.clientWidth-odiv.offsetWidth){
left=document.documentElement.clientWidth-odiv.offsetWidth
}
if(top<0){
top=0;
}else if (top>document.documentElement.clientHeight-odiv.offsetHeight){
top=document.documentElement.clientHeight-odiv.offsetHeight
}
odiv.style.left=left+"px";
odiv.style.top=top+"px";
}
}
document.οnmοuseup=function(){
document.οnmοusemοve=null;
odiv.οnmοuseup=null;
}
}
</script>
</head>
<body>
<div id="a1"></div>
</body>
</html>