网站utf8乱码,网站建设私单,邢台网站建设与制作,音乐网站怎么做精准关键词今天主要是分享一下MutationObserver和IntersectionObserver的区别#xff0c;希望对大家有帮助! MutationObserver 和 IntersectionObserver 的区别 MutationObserver 作用#xff1a;用于监听 DOM 树的变动#xff0c;包括#xff1a;元素的属性、子元素列表或节点文本的…今天主要是分享一下MutationObserver和IntersectionObserver的区别希望对大家有帮助! MutationObserver 和 IntersectionObserver 的区别 MutationObserver 作用用于监听 DOM 树的变动包括元素的属性、子元素列表或节点文本的变化。适用场景可以用来检测 DOM 的结构和内容变化比如元素被插入或删除、属性被更改等。性能由于 MutationObserver 监听的是整个 DOM 树的变化频繁的 DOM 操作会导致性能问题因此适用于较少变化的场景。 IntersectionObserver 作用用于监听目标元素与其祖先元素或 viewport之间的交叉状态即是否进入或离开视口。适用场景适合用于检测元素是否在视口中例如实现图片懒加载、无限滚动或曝光监测。性能由于它的监听目标是元素的可见性相较于 MutationObserver更适合频繁变化的场景。 特性MutationObserverIntersectionObserver监听对象DOM 节点的结构、属性或文本变化目标元素与视口或指定元素的交叉状态常见使用场景检测 DOM 变化插入、删除、修改图片懒加载、曝光监测、滚动加载等性能频繁变化可能影响性能更适合高频率变化的监听 应用场景 IntersectionObserver 在之前我分享的Vue3如何优雅地加载图片的时候其实是使用过 IntersectionObserver 的。会使用它检测 DOMimg 是否可见以此来判断是否需要加载对应的图片 // 懒加载图片的回调函数包含淡入效果和错误处理
function lazyLoadImages(entries, observer) {entries.forEach(entry {// 检查图片是否进入视口if (entry.isIntersecting) {const img entry.target;img.src img.dataset.src; // 将 src 替换为 src 开始加载图片// 图片加载成功后添加 loaded 类触发淡入效果img.onload () img.classList.add(loaded); // 图片加载失败时显示默认占位图img.onerror () img.src placeholder.jpg; observer.unobserve(img); // 停止观察该图片}});
}// 创建 IntersectionObserver 实例用于懒加载
const imageObserver new IntersectionObserver(lazyLoadImages, { threshold: 0.1 });// 选取所有带有 src 属性的图片并开始观察
document.querySelectorAll(img[src]).forEach(img {imageObserver.observe(img);
});除此之外IntersectionObserver 在 性能检测 中也有应用场景。 // 处理元素可见性变化的回调函数
function handleIntersection(entries, observer) {entries.forEach(entry {// 检查元素是否进入视口if (entry.isIntersecting) {console.log(元素已进入视口:, entry.target);// 调用自定义追踪事件函数记录元素可见性trackEvent(element_visible, { elementId: entry.target.id });// 可选停止观察该元素仅触发一次observer.unobserve(entry.target);}});
}// 创建 IntersectionObserver 实例
const observer new IntersectionObserver(handleIntersection, {root: null, // 使用视口作为容器threshold: 0.5 // 当元素 50% 可见时触发回调
});// 选择需要观察的目标元素
const targetElement document.getElementById(target);
observer.observe(targetElement);// 示例追踪事件函数
function trackEvent(eventType, details) {console.log(记录事件: ${eventType}, details);// 在这里将追踪数据发送到服务器或分析服务
}MutationObserver MutationObserver 主要 监听 DOM 的动态变化添加、删除 等。在 SPA 应用中动态加载的场景下会非常有用。 比如我们做一个评论提交的功能当用户提交一条新评论时我们希望检测到 DOM 变化并触发相关操作 div idcomments-sectionp评论列表:/pdiv idcommentsp用户1: 很棒的文章/p/div
/divbutton onclickaddComment()添加评论/buttonscript// 模拟添加评论function addComment() {const comment document.createElement(p);comment.textContent 用户${Date.now()}: 新的评论内容;document.getElementById(comments).appendChild(comment);}// MutationObserver 实例const commentsSection document.getElementById(comments);const observer new MutationObserver((mutationsList) {mutationsList.forEach((mutation) {if (mutation.type childList) {// 调用自定义追踪事件函数记录元素可见性trackEvent(element_update, { elementId: target.target.id });}});});// 观察评论区的子节点变化observer.observe(commentsSection, {childList: true, // 监听子节点变化});
/script 文章转载自: http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.csnmd.cn.gov.cn.csnmd.cn http://www.morning.dzfwb.cn.gov.cn.dzfwb.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.yrlfy.cn.gov.cn.yrlfy.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.qnhpq.cn.gov.cn.qnhpq.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.gctgc.cn.gov.cn.gctgc.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.jmbgl.cn.gov.cn.jmbgl.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.kngqd.cn.gov.cn.kngqd.cn http://www.morning.dmzfz.cn.gov.cn.dmzfz.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.qlwfz.cn.gov.cn.qlwfz.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.znrlg.cn.gov.cn.znrlg.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.ymqfx.cn.gov.cn.ymqfx.cn http://www.morning.tnzwm.cn.gov.cn.tnzwm.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn http://www.morning.pcxgj.cn.gov.cn.pcxgj.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.mbprq.cn.gov.cn.mbprq.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.fmrd.cn.gov.cn.fmrd.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn