forked from zhangmengxue/Fragment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdragSquare.html
168 lines (143 loc) · 4.29 KB
/
dragSquare.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset=utf-8>
<title>可改变大小的正方形demo</title>
<style>
.edit{
position: absolute;
width: 200px;
height: 200px;
border:1px solid #000;
cursor: move;
}
.atop,.abottom,.aleft,.aright{
position: absolute;
width: 5px;
height: 5px;
border:1px solid #000;
background: #fff;
}
.atop{
left: 50%;
top: -4px;
margin-left: -2.5px;
cursor: ns-resize;
}
.abottom{
left: 50%;
bottom: -4px;
margin-left: -2.5px;
cursor: ns-resize;
}
.aleft{
top: 50%;
left: -4px;
margin-top: -2.5px;
cursor: ew-resize;
}
.aright{
top: 50%;
right: -4px;
margin-top: -2.5px;
cursor: ew-resize;
}
</style>
<script type="text/javascript" src="https://g.alicdn.com/kissy/k/1.4.8/seed-min.js"></script>
</head>
<body>
<div class="edit">
<i class="atop"></i>
<i class="abottom"></i>
<i class="aleft"></i>
<i class="aright"></i>
</div>
<script>
KISSY.use(['node','event'],function(S,Node,Event){
var $ = Node.all;
//移动方块(移动后事件需要绑定在document上,因为例子中的dom元素使用了绝对定位,不能绑定在body上,不然会出问题)
var $obj = $('.edit');
$obj.on('mousedown',function(e){
var sX = (e.pageX || e.clientX) - parseInt($obj.offset().left),
sY = (e.pageY || e.clientY) - parseInt($obj.offset().top);
$(document).on('mousemove',function(e){
$(document).on('mouseup mouseleave',function(e){
Event.detach($(document),'mouseup mouseleave mousemove');
});
var left = (e.pageX || e.clientX) - sX + 'px',
right = (e.pageY || e.clientY) -sY + 'px';
$obj.css('left',left);
$obj.css('top',right);
});
});
//更改方块大小
$right = $('.aright');
var width = parseInt($obj.css('width'));
$right.on('mousedown',function(e){
e.halt();
var lX = (e.pageX || e.clientX);
console.log('rightdown');
$(document).on('mousemove',function(e){
var right = (e.pageX || e.clientX) - lX;
lX = (e.pageX || e.clientX);
width = width + right;
$obj.css('width',width+'px');
$(document).on('mouseup',function(e){
$(document).detach('mouseup mousemove');
});
});
});
$left = $('.aleft');
var width = parseInt($obj.css('width'));
$left.on('mousedown',function(e){
e.halt();
var lX = (e.pageX || e.clientX);
var originLeft = $left.offset().left;
$(document).on('mousemove',function(e){
var left = (e.pageX || e.clientX) - lX;
lX = (e.pageX || e.clientX);
$obj.css('left', lX + 'px');
width = width - left;
$obj.css('width',width +'px');
$(document).on('mouseup',function(e){
$(document).detach('mouseup mousemove');
});
});
});
$bottom = $('.abottom');
var height = parseInt($obj.css('height'));
$bottom.on('mousedown',function(e){
e.halt();
var lY = (e.pageY || e.clientY);
console.log('bottomdown');
$(document).on('mousemove',function(e){
var bottom = (e.pageY || e.clientY) - lY;
lY = (e.pageY || e.clientY);
height = height + bottom;
$obj.css('height',height+'px');
$(document).on('mouseup',function(e){
$(document).detach('mouseup mousemove');
});
});
});
$top = $('.atop');
var height = parseInt($obj.css('height'));
$top.on('mousedown',function(e){
e.halt();
var lY = (e.pageY || e.clientY);
var origintop = $top.offset().top;
$(document).on('mousemove',function(e){
var top = (e.pageY || e.clientY) - lY;
lY = (e.pageY || e.clientY);
$obj.css('top', lY + 'px');
height = height - top;
$obj.css('height',height +'px');
$(document).on('mouseup',function(e){
$(document).detach('mouseup mousemove');
});
});
});
});
</script>
</body>
</html>