Da alte Browser wie der Internet Explorer oder auch der frühe Edge den IntersectionObserver oder ResizeObserver noch nicht zur Verfügung stellen, muss für diese Browser noch jeweils ein Polyfill her.
Dies ist nur ein unvollständiger Polyfill, jedoch reicht er für all meine Scripte, die ich auf dieser Website veröffentliche.
IntersectionObserver JavaScript (3,54 kByte) 24.12.2021 12:59
(function () {
'use strict' ;
if (('IntersectionObserver' in window )) {
let IntersectionObserver = function (callback, options) {
var self = this ;
var domObserver = null ;
var observables = [];
var active = false ;
function trigger(target) {
let els = target ? [ target ] : observables, len = els.length, entries = [];
for (let i = 0 ; i < len; i++ ) {
let rect = els[i].getBoundingClientRect();
let intersecting = (rect.top <= window .innerHeight && rect.top >= - rect.height &&
rect.left <= window .innerWidth && rect.left >= - rect.width &&
! ! (els[i].offsetWidth || els[i].offsetHeight || els[i].getClientRects().length));
if (intersecting || els[i].dataset.intersecting != intersecting+ '' ) {
entries.push({time: performance.now(), target: els[i], boundingClientRect: rect, isIntersecting: intersecting});
els[i].dataset.intersecting = intersecting;
}
}
callback.apply(self, [entries, self]);
active = false ;
}
function debounceTrigger() {
if (! active) {
active = true ;
window .requestAnimationFrame( trigger.bind(window , null ));
}
}
this .observe = function (target) {
if (target instanceof window .Element) {
if (observables.length === 0 ) {
connect();
}
var index = observables.indexOf(target);
if (index === - 1 ) {
observables.push(target);
}
trigger(target);
}
};
this .unobserve = function (target) {
if (target instanceof window .Element) {
var index = observables.indexOf(target);
if (index !== - 1 ) {
observables.splice(index, 1 );
}
if (observables.length === 0 ) {
this .disconnect();
}
}
};
function connect () {
document .addEventListener( 'scroll' , debounceTrigger);
window .addEventListener( 'resize' , debounceTrigger);
domObserver = new MutationObserver(debounceTrigger);
domObserver.observe(document , {
attributes: true ,
childList: true ,
characterData: true ,
subtree: true
});
}
this .disconnect = function () {
document .removeEventListener( 'scroll' , debounceTrigger);
window .removeEventListener( 'resize' , debounceTrigger);
domObserver.disconnect();
};
};
window .IntersectionObserver = IntersectionObserver;
}
})();
Auch dies ist nur ein unvollständiger Polyfill, aber auch hier reicht er für all meine Scripte, die ich auf dieser Website veröffentliche.
ResizeObserver JavaScript (3,1 kByte) 24.12.2021 12:59
(function () {
'use strict' ;
if (! ('ResizeObserver' in window )) {
let ResizeObserver = function (callback) {
var domObserver = null ;
var observables = [];
var rects = [];
var active = false ;
function trigger(target) {
let els = target ? [ target ] : observables, len = els.length, entries = [];
for (let i = 0 ; i < len; i++ ) {
let rect = els[i].getBoundingClientRect();
if (! rects[i] || rects[i].width != rect.width || rects[i].height != rects.height) {
if (! rects[i]) {
rects.push(rect);
} else {
rects.splice(i, 1 , rect);
}
entries.push({target: els[i], contentRect: rect});
}
}
callback.apply(self, [entries, self]);
active = false ;
}
function debounceTrigger() {
if (! active) {
active = true ;
window .requestAnimationFrame( trigger.bind(window , null ));
}
}
this .observe = function (target) {
if (target instanceof window .Element) {
if (observables.length === 0 ) {
connect();
}
elements.push(target);
trigger(target);
}
};
this .unobserve = function (target) {
if (target instanceof window .Element) {
var index = elements.indexOf(target);
if (index !== - 1 ) {
elements.splice(index, 1 );
}
if (observables.length === 0 ) {
this .disconnect();
}
}
};
function connect() {
document .addEventListener( 'transitionend' , debounceTrigger);
window .addEventListener( 'resize' , debounceTrigger);
domObserver = new MutationObserver(debounceTrigger);
domObserver.observe(document , {
attributes: true ,
childList: true ,
characterData: true ,
subtree: true
});
};
this .disconnect = function () {
document .removeEventListener( 'transitionend' , debounceTrigger);
window .removeEventListener( 'resize' , debounceTrigger);
domObserver.disconnect();
};
};
window .ResizeObserver = ResizeObserver;
}
})();