58 lines
1.5 KiB
JavaScript
58 lines
1.5 KiB
JavaScript
var index = (function (element, listener) {
|
|
var expand = document.createElement('_');
|
|
var shrink = expand.appendChild(document.createElement('_'));
|
|
var expandChild = expand.appendChild(document.createElement('_'));
|
|
var shrinkChild = shrink.appendChild(document.createElement('_'));
|
|
|
|
var lastWidth = void 0,
|
|
lastHeight = void 0;
|
|
|
|
shrink.style.cssText = expand.style.cssText = 'height:100%;left:0;opacity:0;overflow:hidden;pointer-events:none;position:absolute;top:0;transition:0s;width:100%;z-index:-1';
|
|
shrinkChild.style.cssText = expandChild.style.cssText = 'display:block;height:100%;transition:0s;width:100%';
|
|
shrinkChild.style.width = shrinkChild.style.height = '200%';
|
|
|
|
element.appendChild(expand);
|
|
|
|
test();
|
|
|
|
return stop;
|
|
|
|
function test() {
|
|
unbind();
|
|
|
|
var width = element.offsetWidth;
|
|
var height = element.offsetHeight;
|
|
|
|
if (width !== lastWidth || height !== lastHeight) {
|
|
lastWidth = width;
|
|
lastHeight = height;
|
|
|
|
expandChild.style.width = width * 2 + 'px';
|
|
expandChild.style.height = height * 2 + 'px';
|
|
|
|
expand.scrollLeft = expand.scrollWidth;
|
|
expand.scrollTop = expand.scrollHeight;
|
|
shrink.scrollLeft = shrink.scrollWidth;
|
|
shrink.scrollTop = shrink.scrollHeight;
|
|
|
|
listener({ width: width, height: height });
|
|
}
|
|
|
|
shrink.addEventListener('scroll', test);
|
|
expand.addEventListener('scroll', test);
|
|
}
|
|
|
|
function unbind() {
|
|
shrink.removeEventListener('scroll', test);
|
|
expand.removeEventListener('scroll', test);
|
|
}
|
|
|
|
function stop() {
|
|
unbind();
|
|
|
|
element.removeChild(expand);
|
|
}
|
|
});
|
|
|
|
module.exports = index;
|