Skip to content

Commit 3e25384

Browse files
authored
Merge pull request #2 from movedoa/master
Pull movedoa's work
2 parents a1ec6e4 + 2394495 commit 3e25384

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

src/ResizeSensor.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
};
9595

9696
var i, j;
97-
this.call = function() {
97+
this.call = function(sizeInfo) {
9898
for (i = 0, j = q.length; i < j; i++) {
99-
q[i].call();
99+
q[i].call(this, sizeInfo);
100100
}
101101
};
102102

@@ -152,32 +152,50 @@
152152
var expand = element.resizeSensor.childNodes[0];
153153
var expandChild = expand.childNodes[0];
154154
var shrink = element.resizeSensor.childNodes[1];
155-
var dirty, rafId, newWidth, newHeight;
155+
156+
var dirty, rafId;
156157
var size = getElementSize(element);
157158
var lastWidth = size.width;
158159
var lastHeight = size.height;
159-
160+
var initialHiddenCheck = true, resetRAF_id;
161+
162+
163+
var resetExpandShrink_ = function () {
164+
expandChild.style.width = '100000px';
165+
expandChild.style.height = '100000px';
166+
167+
expand.scrollLeft = 100000;
168+
expand.scrollTop = 100000;
169+
170+
shrink.scrollLeft = 100000;
171+
shrink.scrollTop = 100000;
172+
};
160173
var reset = function() {
161-
//set display to block, necessary otherwise hidden elements won't ever work
162-
var invisible = element.offsetWidth === 0 && element.offsetHeight === 0;
163-
164-
if (invisible) {
165-
var saveDisplay = element.style.display;
166-
element.style.display = 'block';
167-
}
168-
169-
expandChild.style.width = '100000px';
170-
expandChild.style.height = '100000px';
171-
172-
expand.scrollLeft = 100000;
173-
expand.scrollTop = 100000;
174-
175-
shrink.scrollLeft = 100000;
176-
shrink.scrollTop = 100000;
177-
178-
if (invisible) {
179-
element.style.display = saveDisplay;
180-
}
174+
// Check if element is hidden
175+
if (initialHiddenCheck){
176+
if (!expand.scrollTop && !expand.scrollLeft) {
177+
178+
// reset
179+
resetExpandShrink_();
180+
181+
// Check in next frame
182+
if (!resetRAF_id){
183+
resetRAF_id = requestAnimationFrame(function(){
184+
resetRAF_id = 0;
185+
186+
reset();
187+
});
188+
}
189+
190+
return;
191+
}
192+
// Stop checking
193+
else{
194+
initialHiddenCheck = false;
195+
}
196+
}
197+
198+
resetExpandShrink_();
181199
};
182200
element.resizeSensor.resetSensor = reset;
183201

@@ -186,19 +204,21 @@
186204

187205
if (!dirty) return;
188206

189-
lastWidth = newWidth;
190-
lastHeight = newHeight;
207+
lastWidth = size.width;
208+
lastHeight = size.height;
191209

192210
if (element.resizedAttached) {
193-
element.resizedAttached.call();
211+
element.resizedAttached.call(
212+
{
213+
width: lastWidth,
214+
height: lastHeight
215+
});
194216
}
195217
};
196218

197219
var onScroll = function() {
198-
var size = getElementSize(element);
199-
var newWidth = size.width;
200-
var newHeight = size.height;
201-
dirty = newWidth != lastWidth || newHeight != lastHeight;
220+
size = getElementSize(element);
221+
dirty = size.width !== lastWidth || size.height !== lastHeight;
202222

203223
if (dirty && !rafId) {
204224
rafId = requestAnimationFrame(onResized);
@@ -218,8 +238,8 @@
218238
addEvent(expand, 'scroll', onScroll);
219239
addEvent(shrink, 'scroll', onScroll);
220240

221-
// Fix for custom Elements
222-
requestAnimationFrame(reset);
241+
// Fix for custom Elements
242+
requestAnimationFrame(reset);
223243
}
224244

225245
forEachElement(element, function(elem){

0 commit comments

Comments
 (0)