|
6 | 6 | ;
|
7 | 7 | (function() {
|
8 | 8 |
|
| 9 | + var requestAnimationFrame = window.requestAnimationFrame || |
| 10 | + window.mozRequestAnimationFrame || |
| 11 | + window.webkitRequestAnimationFrame || |
| 12 | + function (fn) { |
| 13 | + return window.setTimeout(fn, 20); |
| 14 | + }; |
| 15 | + |
9 | 16 | /**
|
10 | 17 | * Class for dimension change detection.
|
11 | 18 | *
|
|
77 | 84 | '</div>';
|
78 | 85 | element.appendChild(element.resizeSensor);
|
79 | 86 |
|
80 |
| - if (!{fixed: 1, absolute: 1}[getComputedStyle(element, 'position')]) { |
| 87 | + if (getComputedStyle(element, 'position') == 'static') { |
81 | 88 | element.style.position = 'relative';
|
82 | 89 | }
|
83 | 90 |
|
84 | 91 | var expand = element.resizeSensor.childNodes[0];
|
85 | 92 | var expandChild = expand.childNodes[0];
|
86 | 93 | var shrink = element.resizeSensor.childNodes[1];
|
87 |
| - var shrinkChild = shrink.childNodes[0]; |
88 |
| - |
89 |
| - var lastWidth, lastHeight; |
90 | 94 |
|
91 | 95 | 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; |
| 96 | + expandChild.style.width = 100000 + 'px'; |
| 97 | + expandChild.style.height = 100000 + 'px'; |
| 98 | + |
| 99 | + expand.scrollLeft = 100000; |
| 100 | + expand.scrollTop = 100000; |
| 101 | + |
| 102 | + shrink.scrollLeft = 100000; |
| 103 | + shrink.scrollTop = 100000; |
100 | 104 | };
|
101 | 105 |
|
102 | 106 | reset();
|
| 107 | + var dirty = false; |
103 | 108 |
|
104 |
| - var changed = function() { |
105 |
| - if (element.resizedAttached) { |
| 109 | + var dirtyChecking = function() { |
| 110 | + if (!element.resizedAttached) return; |
| 111 | + |
| 112 | + if (dirty) { |
106 | 113 | element.resizedAttached.call();
|
| 114 | + dirty = false; |
107 | 115 | }
|
| 116 | + |
| 117 | + requestAnimationFrame(dirtyChecking); |
| 118 | + }; |
| 119 | + |
| 120 | + requestAnimationFrame(dirtyChecking); |
| 121 | + var lastWidth, lastHeight; |
| 122 | + var cachedWidth, cachedHeight; //useful to not query offsetWidth twice |
| 123 | + |
| 124 | + var onScroll = function() { |
| 125 | + if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) { |
| 126 | + dirty = true; |
| 127 | + |
| 128 | + lastWidth = cachedWidth; |
| 129 | + lastHeight = cachedHeight; |
| 130 | + } |
| 131 | + reset(); |
108 | 132 | };
|
109 | 133 |
|
110 | 134 | var addEvent = function(el, name, cb) {
|
|
115 | 139 | }
|
116 | 140 | };
|
117 | 141 |
|
118 |
| - var onScroll = function() { |
119 |
| - if (element.offsetWidth != lastWidth || element.offsetHeight != lastHeight) { |
120 |
| - changed(); |
121 |
| - } |
122 |
| - reset(); |
123 |
| - }; |
124 |
| - |
125 | 142 | addEvent(expand, 'scroll', onScroll);
|
126 | 143 | addEvent(shrink, 'scroll', onScroll);
|
127 | 144 | }
|
|
0 commit comments