-
Notifications
You must be signed in to change notification settings - Fork 0
/
drag.html
46 lines (44 loc) · 1.54 KB
/
drag.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>drag</title>
<script src="js/jquery-1.12.4.js"></script>
<script>
$(function(){
var $box = $("#box");
$box.mousedown(function(ev){
var disX = ev.pageX - $box.offset().left;
var disY = ev.pageY - $box.offset().top;
$(document).mousemove(function(ev){
var _l = ev.pageX - disX ;
var _t = ev.pageY - disY ;
if(_l<0){
_l=0;
}else if(_l>$(document).innerWidth() - $box.innerWidth()){
_l=$(document).innerWidth() - $box.innerWidth();
}
if(_t<0){
_t=0;
}else if(_t>$(document).innerHeight()-$box.innerHeight()){
_t=$(document).innerHeight()-$box.innerHeight();
}
$box.css({"left":_l,"top":_t});
});
$(document).mouseup(function(){
$(document).unbind("mousemove");
$(document).unbind("mouseup");
});
return false;
});
});
</script>
<style type="text/css">
*{margin: 0;padding: 0;}
#box{width: 200px;height: 200px;background: rgba(0,0,0,.1);position: absolute;left: 0;top: 0;cursor: move;}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>