就是理由新增的 api IntersectionObserver
可以引入下方链接适配兼容
https://cdn.jsdelivr.net/npm/intersection-observer-polyfill@0.1.0/dist/IntersectionObserver.min.js
用法
const monitorVisualArea = (elementArray, callback) => { try { const observer = new IntersectionObserver((mutaions) => { const interArr = mutaions.filter((item) => item.isIntersecting); if (interArr.length) { callback(interArr); } }); for (let i = 0; i < elementArray.length; i++) { observer.observe(elementArray[i]); } } catch (e) { console.log(e); } };
|
const img1 = document.querySelector("#img1"); monitorVisualArea([img1], () => { console.log("显示"); });
|