-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjquery.drag.js
82 lines (73 loc) · 2.38 KB
/
jquery.drag.js
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
/**
* jQuery drag plugin
* @author jQuery / zhaoxianlie
*/
var jDrag = (function($, undefined){
/**
* 可拖拽的节点封装
* @param options
*/
var DraggableClass = function draggable(options){
if(!options.handle)
return;
var s = $.extend({}, options);
var handle = $(s.handle);
var target = s.target && $(s.target);
var currPos = null;
var type = (s.type || '').toString().toUpperCase();
handle[0].onselectstart = function() { return false; }
handle.attr( "unselectable", "on" ).css( "MozUserSelect", "none" );
var _onDrag = null, _dragging = null;
var _mouseMove = function(evt){
var x = evt.pageX - _dragging[0],
y = evt.pageY - _dragging[1];
if(target){
var css = {};
if(type !== 'Y')
css.left = currPos.left + x;
if(type !== 'X')
css.top = currPos.top + y;
if(!$.isEmptyObject(css))
target.css(css);
}
if($.isFunction(s.onMove))
s.onMove(evt, x, y);
};
var _mouseUp = function(evt){
$(document).unbind("mousemove", _mouseMove).unbind("mouseup", _mouseUp);
_onDrag = false;
if($.isFunction(s.onUp))
s.onUp(evt);
};
var _mouseDown = function(evt){
if(_onDrag)
_mouseUp();
if($.isFunction(s.onDown))
s.onDown(evt);
if(target)
currPos = {left: target[0].offsetLeft, top: target[0].offsetTop};
_dragging = [evt.pageX, evt.pageY];
$(document).bind("mousemove", _mouseMove).bind("mouseup", _mouseUp);
};
s.handle.bind('mousedown', _mouseDown);
this.remove = function(){
$(document).unbind("mousemove", _mouseMove);
$(document).unbind("mousemove", _mouseUp);
$(document).unbind("mousedown", _mouseDown);
}
this.destroy = function () {
handle.unbind('mousedown', _mouseDown);
}
}
/**
* 初始化一个可拖拽的节点
* @param options
* @private
*/
var _init = function(options){
return new DraggableClass(options);
};
return {
init : _init
};
})(jQuery);