|
1 |
| -;(function() { |
| 1 | +/** |
| 2 | + * Copyright Marc J. Schmidt. See the LICENSE file at the top-level |
| 3 | + * directory of this distribution and at |
| 4 | + * https://github.com/marcj/css-element-queries/blob/master/LICENSE. |
| 5 | + */ |
| 6 | +; |
| 7 | +(function() { |
2 | 8 |
|
3 | 9 | /**
|
4 | 10 | * Class for dimension change detection.
|
|
9 | 15 | * @constructor
|
10 | 16 | */
|
11 | 17 | this.ResizeSensor = function(element, callback) {
|
12 |
| - /** |
13 |
| - * Adds a listener to the over/under-flow event. |
14 |
| - * |
15 |
| - * @param {HTMLElement} element |
16 |
| - * @param {Function} callback |
17 |
| - */ |
18 |
| - function addResizeListener(element, callback) { |
19 |
| - if (window.OverflowEvent) { |
20 |
| - //webkit |
21 |
| - element.addEventListener('overflowchanged', function(e) { |
22 |
| - callback.call(this, e); |
23 |
| - }); |
24 |
| - } else { |
25 |
| - element.addEventListener('overflow', function(e) { |
26 |
| - callback.call(this, e); |
27 |
| - }); |
28 |
| - element.addEventListener('underflow', function(e) { |
29 |
| - callback.call(this, e); |
30 |
| - }); |
31 |
| - } |
32 |
| - } |
33 |
| - |
34 | 18 | /**
|
35 | 19 | *
|
36 | 20 | * @constructor
|
|
78 | 62 | return;
|
79 | 63 | }
|
80 | 64 |
|
81 |
| - if ('onresize' in element && 11 > document.documentMode) { |
82 |
| - //internet explorer up to 10 |
83 |
| - if (element.attachEvent) { |
84 |
| - element.attachEvent('onresize', function() { |
85 |
| - element.resizedAttached.call(); |
86 |
| - }); |
87 |
| - } else if (element.addEventListener) { |
88 |
| - element.addEventListener('resize', function(){ |
89 |
| - element.resizedAttached.call(); |
90 |
| - }); |
91 |
| - } |
92 |
| - } else { |
93 |
| - var myResized = function() { |
94 |
| - if (setupSensor()) { |
95 |
| - element.resizedAttached.call(); |
96 |
| - } |
97 |
| - }; |
98 |
| - element.resizeSensor = document.createElement('div'); |
99 |
| - element.resizeSensor.className = 'resize-sensor'; |
100 |
| - var style = |
101 |
| - 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1;'; |
102 |
| - element.resizeSensor.style.cssText = style; |
103 |
| - element.resizeSensor.innerHTML = |
104 |
| - '<div class="resize-sensor-overflow" style="' + style + '">' + |
105 |
| - '<div></div>' + |
106 |
| - '</div>' + |
107 |
| - '<div class="resize-sensor-underflow" style="' + style + '">' + |
108 |
| - '<div></div>' + |
109 |
| - '</div>'; |
110 |
| - element.appendChild(element.resizeSensor); |
111 |
| - |
112 |
| - if ('absolute' !== getComputedStyle(element, 'position')) { |
113 |
| - element.style.position = 'relative'; |
| 65 | + element.resizeSensor = document.createElement('div'); |
| 66 | + element.resizeSensor.className = 'resize-sensor'; |
| 67 | + var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: scroll; z-index: -1; visibility: hidden;'; |
| 68 | + var styleChild = 'position: absolute; left: 0; top: 0;'; |
| 69 | + |
| 70 | + element.resizeSensor.style.cssText = style; |
| 71 | + element.resizeSensor.innerHTML = |
| 72 | + '<div class="resize-sensor-expand" style="' + style + '">' + |
| 73 | + '<div style="' + styleChild + '"></div>' + |
| 74 | + '</div>' + |
| 75 | + '<div class="resize-sensor-shrink" style="' + style + '">' + |
| 76 | + '<div style="' + styleChild + ' width: 200%; height: 200%"></div>' + |
| 77 | + '</div>'; |
| 78 | + element.appendChild(element.resizeSensor); |
| 79 | + |
| 80 | + if ('absolute' !== getComputedStyle(element, 'position')) { |
| 81 | + element.style.position = 'relative'; |
| 82 | + } |
| 83 | + |
| 84 | + var expand = element.resizeSensor.childNodes[0]; |
| 85 | + var expandChild = expand.childNodes[0]; |
| 86 | + var shrink = element.resizeSensor.childNodes[1]; |
| 87 | + var shrinkChild = shrink.childNodes[0]; |
| 88 | + |
| 89 | + var lastWidth, lastHeight; |
| 90 | + |
| 91 | + var reset = function() { |
| 92 | + expandChild.style.width = expand.offsetWidth + 10 + 'px'; |
| 93 | + expandChild.style.height = expand.offsetHeight + 10 + 'px'; |
| 94 | + expand.scrollLeft = expand.scrollWidth; |
| 95 | + expand.scrollTop = expand.scrollHeight; |
| 96 | + shrink.scrollLeft = shrink.scrollWidth; |
| 97 | + shrink.scrollTop = shrink.scrollHeight; |
| 98 | + lastWidth = element.offsetWidth; |
| 99 | + lastHeight = element.offsetHeight; |
| 100 | + }; |
| 101 | + |
| 102 | + reset(); |
| 103 | + |
| 104 | + var changed = function() { |
| 105 | + element.resizedAttached.call(); |
| 106 | + }; |
| 107 | + |
| 108 | + var addEvent = function(el, name, cb) { |
| 109 | + if (el.attachEvent) { |
| 110 | + el.attachEvent('on' + name, cb); |
| 111 | + } else { |
| 112 | + el.addEventListener(name, cb); |
114 | 113 | }
|
| 114 | + }; |
115 | 115 |
|
116 |
| - var x = 0, |
117 |
| - y = 0, |
118 |
| - firstStyle = element.resizeSensor.firstElementChild.firstChild.style, |
119 |
| - lastStyle = element.resizeSensor.lastElementChild.firstChild.style; |
120 |
| - |
121 |
| - function setupSensor() { |
122 |
| - var change = false, |
123 |
| - width = element.resizeSensor.offsetWidth, |
124 |
| - height = element.resizeSensor.offsetHeight; |
125 |
| - |
126 |
| - if (x != width) { |
127 |
| - firstStyle.width = (width - 1) + 'px'; |
128 |
| - lastStyle.width = (width + 1) + 'px'; |
129 |
| - change = true; |
130 |
| - x = width; |
131 |
| - } |
132 |
| - if (y != height) { |
133 |
| - firstStyle.height = (height - 1) + 'px'; |
134 |
| - lastStyle.height = (height + 1) + 'px'; |
135 |
| - change = true; |
136 |
| - y = height; |
137 |
| - } |
138 |
| - return change; |
| 116 | + addEvent(expand, 'scroll', function() { |
| 117 | + if (element.offsetWidth > lastWidth || element.offsetHeight > lastHeight) { |
| 118 | + changed(); |
139 | 119 | }
|
| 120 | + reset(); |
| 121 | + }); |
140 | 122 |
|
141 |
| - setupSensor(); |
142 |
| - addResizeListener(element.resizeSensor, myResized); |
143 |
| - addResizeListener(element.resizeSensor.firstElementChild, myResized); |
144 |
| - addResizeListener(element.resizeSensor.lastElementChild, myResized); |
145 |
| - } |
| 123 | + addEvent(shrink, 'scroll',function() { |
| 124 | + if (element.offsetWidth < lastWidth || element.offsetHeight < lastHeight) { |
| 125 | + changed(); |
| 126 | + } |
| 127 | + reset(); |
| 128 | + }); |
146 | 129 | }
|
147 | 130 |
|
148 | 131 | if ('array' === typeof element
|
|
0 commit comments