123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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;
|