Browse Source

resizeObserver

es6
guy 2 years ago
parent
commit
b08014b594
  1. 29
      src/core/platform/web/detectElementResize.js

29
src/core/platform/web/detectElementResize.js

@ -112,7 +112,21 @@
}; };
var addResizeListener = function (element, fn) { var addResizeListener = function (element, fn) {
if (attachEvent) { if (window.ResizeObserver) {
if (!element.__resizeObserver__) {
var resizeObserver = new window.ResizeObserver(function () {
element.__resizeListeners__.forEach(function (listener) {
listener();
});
});
resizeObserver.observe(element);
element.__resizeObserver__ = resizeObserver;
}
if (!element.__resizeListeners__) {
element.__resizeListeners__ = [];
}
element.__resizeListeners__.push(fn);
} else if (attachEvent) {
element.attachEvent("onresize", fn); element.attachEvent("onresize", fn);
BI.nextTick(fn); BI.nextTick(fn);
} else { } else {
@ -137,8 +151,17 @@
} }
}; };
var removeResizeListener = function (element, fn) { var removeResizeListener = function (element, fn) {
if (attachEvent) element.detachEvent("onresize", fn); if (window.ResizeObserver) {
else { var index = element.__resizeListeners__.indexOf(fn);
if (index >= 0) {
element.__resizeListeners__.splice(index, 1);
if (!element.__resizeListeners__.length) {
element.__resizeObserver__ && element.__resizeObserver__.unobserve(element);
}
}
} else if (attachEvent) {
element.detachEvent("onresize", fn);
} else {
element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1); element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
if (!element.__resizeListeners__.length) { if (!element.__resizeListeners__.length) {
element.removeEventListener("scroll", scrollListener); element.removeEventListener("scroll", scrollListener);

Loading…
Cancel
Save