-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (43 loc) · 1.16 KB
/
index.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
require('mutationobserver-shim')
var win
if (typeof window !== 'undefined') {
win = window
} else if (typeof global !== 'undefined') {
win = global
} else if (typeof self !== 'undefined') {
win = self
} else {
win = {}
}
var onresize = ('ResizeObserver' in win) ? resizeObserverOnResize : mutationObserverOnResize
module.exports = onresize
onresize.default = onresize
function mutationObserverOnResize (target, callback) {
function makeid (el) {
return [target.style.width, target.style.height, target.clientWidth, target.clientHeight].join('')
}
var last = makeid(target)
var observer = new MutationObserver(function (mutations) {
for (var i = 0; i < mutations.length; i++) {
var mutation = mutations[i]
if (mutation.attributeName !== 'style') continue
var now = makeid(target)
if (now !== last) {
last = now
callback()
break
}
}
})
observer.observe(target, {
attributes: true,
childList: false,
characterData: false
})
return observer
}
function resizeObserverOnResize (target, callback) {
var observer = new ResizeObserver(callback)
observer.observe(target)
return observer
}