-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraggable.html
More file actions
144 lines (122 loc) · 3.43 KB
/
draggable.html
File metadata and controls
144 lines (122 loc) · 3.43 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>draggable thing</title>
<style>
* {
box-sizing:border-box;
}
#container {
width:1000px;
height:800px;
background-color:#f0f0f0;
border:1px solid #d0d0d0;
position:relative;
overflow:hidden;
}
#container div.item {
position:absolute;
width:150px;
height:100px;
background-color:#ff8080;
border:1px solid #dd6060;
left:100px;
top:100px;
text-align:center;
padding-top:40px;
cursor:move;
}
#container div.item.bounded {
background-color: #8080ff;
border: 1px solid #6060dd;
}
#container div.item.dragging {
background-color:#ffc080;
}
</style>
</head>
<body>
<div>
<button id="btnAdd">Add Bounded Item</button>
<button id="btnAddUnbounded">Add Unbounded Item</button>
</div>
<div id="container">
<div class="item">#1</div>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var itemOffset, containerOffset, containerSize;
$(function () {
containerOffset = $('#container').offset();
containerSize = {
x: $('#container').width(),
y: $('#container').height()
};
$('#btnAdd').on('click', function () {
var l = $('#container div.item').length;
var s = '<div class="item bounded">#' + (l + 1) + '</div>';
$('#container').append(s);
});
$('#btnAddUnbounded').on('click', function () {
var l = $('#container div.item').length;
var s = '<div class="item">#' + (l + 1) + '</div>';
$('#container').append(s);
});
$('#container').on('mousedown', 'div.item', function (e) {
if ($('div.item.dragging').length == 0) {
$(this).addClass('dragging');
itemOffset = {
x: e.offsetX,
y: e.offsetY
};
}
});
$(window).on('mousemove', function (e) {
console.log(e);
if ($('#container div.item.dragging').length > 0) {
//console.log(e.offsetX, e.offsetY);
var containerPos = {
x: e.pageX - containerOffset.left,
y: e.pageY - containerOffset.top
};
//console.log(containerPos);
var edges = {
left: containerPos.x - itemOffset.x,
top: containerPos.y - itemOffset.y,
right: containerPos.x - itemOffset.x + $('#container div.item.dragging').outerWidth(),
bottom: containerPos.y - itemOffset.y + $('#container div.item.dragging').outerHeight()
};
var itemSize = {
x: $('#container div.item.dragging').outerWidth(),
y: $('#container div.item.dragging').outerHeight()
};
if ($('#container div.item.dragging').hasClass('bounded')) {
if (edges.left < 0)
edges.left = 0;
if (edges.right > containerSize.x)
edges.left = containerSize.x - itemSize.x;
if (edges.top < 0)
edges.top = 0;
if (edges.bottom > containerSize.y)
edges.top = containerSize.y - itemSize.y;
}
else {
if (edges.left < -itemSize.x + 10)
edges.left = -itemSize.x + 10;
if (edges.right > containerSize.x + itemSize.x - 10)
edges.left = containerSize.x - 10;
if (edges.top < -itemSize.y + 10)
edges.top = -itemSize.y + 10;
if (edges.bottom > containerSize.y + itemSize.y - 10)
edges.top = containerSize.y -10 ;
}
$('#container div.item.dragging')
.css('left', edges.left + 'px')
.css('top', edges.top + 'px');
}
}).on('mouseup', function () {
$('#container div.item.dragging').removeClass('dragging');
});
});
</script>
</body>
</html>