拖拽使用的三个事件
onmousedown //鼠标按下
onmousemove //鼠标移动
onmouseup //鼠标抬起
<head>
<style>
img{
position:absolute;
left:0;
top:0;
width:200px;
}
</style>
</head>
<body>
<img src="images/1.jpg" alt="">
<script>
var oImg = document.getElementsByTagName('img')[0];
oImg.onmousedown=function(e){
e=e||window.event;
var starX=e.clientX;//记录鼠标按下时的水平位置
var starY=e.clientY;//记录鼠标按下时的竖直位置
var starLeft=this.offsetLeft;//记录鼠标按下时图片的初始水平位置
var starTop=this.offsetTop;//记录鼠标按下时图片的初始竖直位置
document.onmousemove=function(e){
e=e||window.event;
var moveX=e.clientX;//记录鼠标移动时的水平位置
var moveY=e.clientY;//记录鼠标移动时的竖直位置
oImg.style.left=starLeft+e.clientX-starX+"px";//设置图片的left值
oImg.style.top=starTop+e.clientY-starY+"px";//设置图片的top值
}
oImg.onmouseup=function(){
document.onmousemove=null;//
}
}
</script>
</body>