-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize-observer.js
More file actions
executable file
·208 lines (165 loc) · 5.17 KB
/
Copy pathsize-observer.js
File metadata and controls
executable file
·208 lines (165 loc) · 5.17 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
// ==========================================================================
// Project: Size Observer
// Description: Efficiently observe size changes of a jQuery element
// Copyright: ©2012-2018 GestiXi
// License: Licensed under the MIT license (see LICENCE)
// Version: 1.0
// ==========================================================================
!function($) { "use strict";
// ..........................................................
// SIZE OBSERVER PLUGIN DEFINITION
//
var itemsToObserve = [],
count = 0,
timer,
_newSize;
/**
To use this plugin you can directly specify a callback as first
parameter. It will be call each time the size of the element changes.
You can also specify the properties to observes `height` or `width`
as first parameter, and the callback as second parameter.
Example:
$('img').onResize(function() {
console.log('The size of my picture changed');
});
// Same as:
$('img').onResize(['height', 'width'], function() {
console.log('The size of my picture changed');
});
// Only check the height:
$(document).onResize('height', function() {
console.log('The size of my document changed');
});
@param properties
@param callback
*/
$.fn.onResize = function(properties, callback) {
var propertiesType = $.type(properties);
if (propertiesType === 'function') {
callback = properties;
properties = ['height', 'width'];
}
else if (propertiesType === 'string') properties = [properties];
if ($.type(callback) !== 'function') return;
return this.each(function() {
var $this = $(this),
instance = $this.data('onResize');
if (!instance) {
instance = new OnResize(this, properties, callback);
$this.data('onResize', instance);
}
else {
instance.addProperties(properties);
instance.handlers.push(callback);
}
})
}
// ..........................................................
// SIZE OBSERVER PUBLIC CLASS DEFINITION
//
var OnResize = function(element, properties, callback) {
var that = this;
that.element = element;
that.handlers = [callback];
that.addProperties(properties);
itemsToObserve.push(that);
checkItem(that);
}
$.fn.onResize.Constructor = OnResize;
OnResize.prototype = {
constructor: OnResize,
addProperties: function(properties) {
var that = this;
that.properties = $.unique($.merge(that.properties || [], properties));
that.check = that.sizeCheckFunction(that.properties);
},
sizeCheckFunction: function(properties) {
var element = this.element,
targets,
keyList,
array = [];
if (element === window) {
targets = [document.documentElement];
keyList = {
height: 'clientHeight',
width: 'clientWidth'
};
}
else if (element === document) {
targets = [document.body, document.documentElement];
keyList = {
height: ['clientHeight', 'scrollHeight', 'offsetHeight'],
width: ['clientWidth', 'scrollWidth', 'offsetWidth']
};
}
else {
targets = [element];
keyList = {
height: 'clientHeight',
width: 'clientWidth',
top: 'offsetTop',
left: 'offsetLeft',
bottom: 'offsetBottom',
right: 'offsetRight'
};
if ($.inArray('left', properties) !== -1 || $.inArray('offsetLeft', properties) !== -1) {
keyList.push({ t: element, k: 'offsetLeft' });
}
}
for (var i = properties.length - 1; i >= 0; i--) {
var property = properties[i];
for (var j = targets.length - 1; j >= 0; j--) {
var target = targets[j],
keys;
if (keys = keyList[property]) {
if ($.type(keys) !== 'array') keys = [keys];
for (var k = keys.length - 1; k >= 0; k--) {
array.push({ t: target, k: keys[k] });
};
}
else {
array.push({ t: target, k: property });
}
};
};
return function() {
var newSize = '';
for (var i = array.length - 1; i >= 0; i--) {
var key = array[i];
newSize += key.t[key.k]+'-';
};
return this.size !== newSize ? newSize : false;
}
}
};
var checkItem = function(item) {
_newSize = item.check();
if (_newSize !== false) {
if (item.hasOwnProperty('size')) {
for (var i = item.handlers.length - 1; i >= 0; i--) {
item.handlers[i].call(item.element, item);
};
}
item.size = _newSize;
}
};
var run = function () {
for (var i = itemsToObserve.length - 1; i >= 0; i--) {
checkItem(itemsToObserve[i]);
};
timer = setTimeout(run, count < 10 ? 64 : (count < 100 ? 250 : 1000));
count++;
};
var doRun = function() {
if (timer) clearTimeout(timer);
count = 0;
run();
};
doRun();
$(window).on('resize', function() {
doRun();
});
$(window).on('scroll', function() {
doRun();
});
}(window.jQuery);