图片赖加载

function isVisible(el) {
            const position = el.getBoundingClientRect()
            const windowHeight = document.documentElement.clientHeight
            // 顶部边缘可见
            const topVisible = position.top > 0 && position.top < windowHeight;
            // 底部边缘可见
            const bottomVisible = position.bottom < windowHeight && position.bottom > 0;
            return topVisible || bottomVisible;
        }

        function imageLazyLoad() {
            const images = document.querySelectorAll('img')
            for (let img of images) {
                const realSrc = img.dataset.src
                if (!realSrc) continue
                if (isVisible(img)) {
                    img.src = realSrc
                    img.dataset.src = ''
                }
            }
        }

 

posted @ 2021-04-25 16:31  心之所指,行之所至  阅读(53)  评论(0)    收藏  举报