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) {
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);
BI.nextTick(fn);
} else {
@ -137,8 +151,17 @@
}
};
var removeResizeListener = function (element, fn) {
if (attachEvent) element.detachEvent("onresize", fn);
else {
if (window.ResizeObserver) {
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);
if (!element.__resizeListeners__.length) {
element.removeEventListener("scroll", scrollListener);

Loading…
Cancel
Save