-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathjquery.dragView.js
More file actions
294 lines (250 loc) · 11.3 KB
/
jquery.dragView.js
File metadata and controls
294 lines (250 loc) · 11.3 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* dragView.JS
* Copyright 2017, tanhang
* Date: June 20 2017
*/
(function ($) {
var config = {
item: 'view-box',
panel: 'dragView_panel',
module: 'drag_module'
},
item, panel,
tempClass, dashClass, maskClass, headClass, btnClass,
//鼠标距离元素上边距离
mouseY = 0,
//拖拽对象偏移量 top:对象距离可视化区域上边距离
range = { top: 0, y1: 0, y2: 0, y: 0 },
lastPos = { y: 0 }, //拖拽对象的坐标
tarPos = { y: 0 }, //目标元素对象的坐标初始化
theDiv = null, move = false, //拖拽对象 拖拽状态
theDivId = 0, theDivHeight = 0, theDivHalf = 0, tarFirstY = 0, //拖拽对象的索引、高度、的初始化。
tarDiv = null, tarFirst = null, tempDiv = null; //要插入的目标元素的对象, 临时的虚线对象
function loopModule() {
$('.' + config.module).each(function () {
var then = $(this);
then.find('.btn-delete').unbind('click');
then.find('.btn-delete').bind('click', function () {
if (window.confirm('确定要删除此版块?')) {
$(this).parents('.' + config.module).remove()
}
});
then.unbind('mouseover mouseout');
then.bind('mouseover', function () {
$(this).addClass('active');
}).bind('mouseout', function () {
$(this).removeClass('active');
});
then.find('.' + headClass).unbind('mousedown');
then.find('.' + headClass).bind('mousedown', function (e) {
if (move) return;
e.preventDefault();
//拖拽对象
theDiv = $(this).parent();
//鼠标元素相对偏移量
mouseY = e.pageY - theDiv.offset().top;
range.top = theDiv.offset().top - $(panel).offset().top;
range.y1 = e.pageY;
theDivId = theDiv.index();
theDivHeight = theDiv.height();
move = true;
theDiv.attr('class', dashClass);
theDiv.css({ top: range.top + 'px' });
// 创建新元素 插入拖拽元素之前的位置(虚线框)
$('<div class="' + tempClass + '"></div>').insertBefore(theDiv);
tempDiv = $('.' + tempClass); //获得临时 虚线框的对象
moduleSort();
});
});
}
function moduleAdd() {
var dragActive = false,
dragPanel = false,
thisBox, viewHtml,
viewPos = { x: 0, y: 0 },
mousePos = { x: 0, y: 0, moveX: 0, moveY: 0 },
dragViewPos = { x: 0, y: 0 },
//临时拖拽层
dragMask = $('<div id="drag-mask"></div>').appendTo('body'),
dragView = $('<div id="drag-view"></div>').appendTo(dragMask);
// >>> 添加板块
$(item).mousedown(function (e) {
e.preventDefault();
dragActive = true;
thisBox = $(this);
viewPos.x = parseInt($(thisBox).offset().left) - parseInt($(document).scrollLeft());
viewPos.y = parseInt($(thisBox).offset().top) - parseInt($(document).scrollTop());
// ajax获取模板的 html
viewHtml = `
<div class="home-images">
<div class="w clearfix">
<div class="item first">
<a href="#"><img src="images/1.jpg" alt=""></a>
</div>
<div class="item">
<a href="#"><img src="images/2.jpg" alt=""></a>
</div>
<div class="item">
<a href="#"><img src="images/3.jpg" alt=""></a>
</div>
<div class="item">
<a href="#"><img src="images/4.jpg" alt=""></a>
</div>
</div>
</div>`;
$(thisBox).parent().addClass('active');
$(dragView).show();
$(dragMask).show();
$(dragView).css({ 'top': viewPos.y, 'left': viewPos.x });
$(dragView).html(viewHtml);
//鼠标初始位置
mousePos.x = e.pageX - parseInt($(document).scrollLeft());
mousePos.y = e.pageY - parseInt($(document).scrollTop());
//是否放入可视化区域
dragPanel = false;
$(document).mousemove(function (e) {
if (!dragActive) return;
e.preventDefault();
//鼠标移动后的位置,和 mousePos.x mousePos.y 同时减去滚动条的距离 等于 相对窗口的距离
mousePos.moveX = e.pageX - parseInt($(document).scrollLeft());
mousePos.moveY = e.pageY - parseInt($(document).scrollTop());
dragViewPos.x = viewPos.x + (mousePos.moveX - mousePos.x);
dragViewPos.y = viewPos.y + (mousePos.moveY - mousePos.y);
$(dragView).css({ 'top': dragViewPos.y, 'left': dragViewPos.x });
mousePos.moveX = e.pageX;
mousePos.moveY = e.pageY;
//可视化区域位置
var area = { x: panel.offset().left, y: panel.offset().top, w: panel.width(), h: panel.height() };
//判断鼠标是否在可视化区域
if (mousePos.moveX >= area.x && mousePos.moveX <= area.x + area.w && mousePos.moveY >= area.y && mousePos.moveY <= area.y + area.h) {
dragPanel = true;
//参考排序 查找插入目标元素
var $main = $('.' + config.module);
if ($main.length > 0) {
$main.each(function () {
tarDiv = $(this);
tarPos.y = tarDiv.offset().top;
theDivHalf = tarDiv.height() / 2;
// tarPos.y1 = tarPos.y + theDivHalf;
tarFirst = $main.eq(0);
tarFirstHalf = tarFirst.height() / 2;
tarFirstY = tarFirst.offset().top + tarFirstHalf;
if (dragViewPos.y + parseInt($(document).scrollTop()) <= tarFirstY) {
if (tempDiv == null) {
$('<div class="' + tempClass + '"></div>').insertBefore(tarFirst);
tempDiv = $('.' + tempClass);
} else {
tempDiv.insertBefore(tarFirst);
}
return false;
}
if (dragViewPos.y + parseInt($(document).scrollTop()) >= tarPos.y + theDivHalf) {
if (tempDiv == null) {
$('<div class="' + tempClass + '"></div>').insertBefore(tarFirst);
tempDiv = $('.' + tempClass);
} else {
tempDiv.insertAfter(tarDiv);
}
}
})
} else {
if (tempDiv == null) {
$('<div class="' + tempClass + '"></div>').appendTo(panel);
tempDiv = $('.' + tempClass);
}
}
} else {
dragPanel = false;
if (tempDiv != null) {
tempDiv.remove();
tempDiv = null;
}
}
}).mouseup(function (e) {
if (!dragActive) return;
e.preventDefault();
dragActive = false;
if (dragPanel) {
$('<div class="' + config.module + '"><div class="' + maskClass + '"></div><div class="' + headClass + '"></div><div class="' + btnClass + '"><a class="btn-delete" title="删除板块">删除</a><a class="btn-edit" title="编辑板块">编辑</a></div>' + viewHtml + '</div>').insertBefore(tempDiv);
dragPanel = false;
loopModule();
}
if (tempDiv != null) {
tempDiv.remove();
tempDiv == null;
}
//拖拽结束,还原
$(dragView).hide();
$(dragView).html('');
$(dragMask).hide();
$(thisBox).parent().removeClass('active');
$(document).unbind('mousemove mouseup');
})
})
}
function moduleSort() {
$(panel).mousemove(function (e) {
if (!move) return;
e.preventDefault();
range.y2 = e.pageY;
range.y = range.top + (range.y2 - range.y1);
// 拖拽元素随鼠标移动
theDiv.css({ top: range.y + 'px' });
lastPos.y = e.pageY - mouseY;
// lastPos.y1 = lastPos.y + theDivHeight;
// 拖拽元素随鼠标移动 查找插入目标元素
var $main = $('.' + config.module); // 局部变量:按照重新排列过的顺序 再次获取 各个元素的坐标
$main.each(function () {
tarDiv = $(this);
tarPos.y = tarDiv.offset().top;
theDivHalf = tarDiv.height() / 2;
// tarPos.y1 = tarPos.y + theDivHalf;
tarFirst = $main.eq(0); // 获得第一个元素
tarFirstHalf = tarFirst.height() / 2;
tarFirstY = tarFirst.offset().top + tarFirstHalf; // 第一个元素对象的中心纵坐标
//拖拽对象 移动到第一个位置
if (lastPos.y <= tarFirstY) {
tempDiv.insertBefore(tarFirst);
return false;
}
//判断要插入目标元素的 坐标后,直接插入
if (lastPos.y >= tarPos.y + theDivHalf) {
tempDiv.insertAfter(tarDiv);
}
});
}).mouseup(function () {
if (theDiv == null) return;
if (tempDiv != null) {
theDiv.insertBefore(tempDiv); // 拖拽元素插入到 临时div的位置上
tempDiv.remove(); // 删除新建的临时div
}
//初始化,恢复默认样式
theDiv.attr("class", config.module);
theDiv.css({ top: '0px' });
theDiv = tempDiv = null;
move = false;
});
}
this.dragView = {
init: function (options) {
if (options) {
for (var key in options) {
config[key] = options[key];
}
}
item = $('.' + config.item);
panel = $('.' + config.panel);
tempClass = config.module + '_temp';
dashClass = config.module + '_dash';
maskClass = config.module + '_mask';
headClass = config.module + '_head';
btnClass = config.module + '_btn';
loopModule();
moduleAdd();
return this;
},
opts: function () {
return config;
}
}
})(jQuery)