<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<style>
*{margin: 0;padding: 0;}
#box{width: 120px;height: 120px;border: 1px solid #ccc;background: #F5F5F5;position: fixed;left: 100px;top: 100px;}
</style>
</head>
<body>
<div id="box" ></div>
<script>
$(function(){
var box=document.getElementById('box');
var isPressed=false; //声明一个标志鼠标是否按下
var offsetX=0;
var offsetY=0;
box.οnmοusedοwn=function(event){
isPressed=true;
this.style.cursor="move";
offsetX=event.offsetX;
offsetY=event.offsetY;
}
box.οnmοuseup=function(){
isPressed=false;
this.style.cursor="default";
}
box.οnmοusemοve=function(event){
if(!isPressed){
return;
}
box.style.left=event.clientX-offsetX+'px';
box.style.top=event.clientY-offsetY+'px';
}
})
</script>
</body>
</html>
js简单的拖拽事件
最新推荐文章于 2023-10-04 11:08:18 发布