-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.resize.js
95 lines (76 loc) · 1.84 KB
/
jquery.resize.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
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @license MIT http://mu-lib.mit-license.org/
*/
define([ "jquery" ], function ResizeModule($) {
/**
* Normalized resize event for DOM elements. Extends {@link jQuery} with:
*
* - {@link $#event-resize} event
*
* @class mu-lib.jquery.resize
* @static
* @alias plugin.jquery
*/
var NULL = null;
var RESIZE = "resize";
var W = "w";
var H = "h";
var $ELEMENTS = $([]);
var INTERVAL = NULL;
function iterator(index, self) {
// Get data
var $data = $.data(self);
// Get reference to $self
var $self = $(self);
// Get previous width and height
var w = $self.width();
var h = $self.height();
// Check if width or height has changed since last check
if (w !== $data[W] || h !== $data[H]) {
$self.trigger(RESIZE, [$data[W] = w, $data[H] = h]);
}
}
/**
* Internal interval
*/
function interval() {
$ELEMENTS.each(iterator);
}
$.event.special[RESIZE] = {
setup : function () {
var self = this;
// window has a native resize event, exit fast
if ($.isWindow(self)) {
return false;
}
// Store data
var $data = $.data(self, RESIZE, {});
// Get reference to $self
var $self = $(self);
// Initialize data
$data[W] = $self.width();
$data[H] = $self.height();
// Add to tracked collection
$ELEMENTS = $ELEMENTS.add(self);
// If this is the first element, start interval
if($ELEMENTS.length === 1) {
INTERVAL = setInterval(interval, 100);
}
},
teardown : function () {
var self = this;
// window has a native resize event, exit fast
if ($.isWindow(self)) {
return false;
}
// Remove data
$.removeData(self, RESIZE);
// Remove from tracked collection
$ELEMENTS = $ELEMENTS.not(self);
// If this is the last element, stop interval
if($ELEMENTS.length === 0 && INTERVAL !== NULL) {
clearInterval(INTERVAL);
}
}
};
});