1. IntersectionObserver 图片懒加载 虚拟列表
const observer = new IntersectionObserver((entries) => {
// console.log(entries, binding.value)
if (entries[0].intersectionRatio > 0) {
setTimeout(() => {
el.src = binding.value
observer.unobserve(el) // 停止监听
}, 500)
}
})
observer.observe(el) // 开启监听 el: HTMLImageElemnet
2. window.getComputedStyle() 获取DOM元素的样式属性值
这个函数接受两个参数:要获取样式的元素和一个可选的伪元素字符串(例如:before或者null)
const trans = document.querySelector(".trans");
const style = window.getComputedStyle(trans);
console.log(style.width);