zoukankan      html  css  js  c++  java
  • 图片懒加载的实现

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box {
                 500px;
                height: 500px;
                border: 1px solid orange;
                display: flex;
                flex-direction: column;
                align-items: center;
                overflow: scroll;
            }
    
            img {
                 300px;
                height: 300px;
                opacity: 0;
                transition: opacity .6s linear;
            }
            .fade{
                opacity: 1;
            }
        </style>
    </head>
    
    <body>
        <div class="box">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
            <img data-src="https://feed-image.baidu.com/0/pic/1f8728565d98b8747c6c9bfcf5bf1982.jpg" alt="" srcset="">
        </div>
        <script>
            let box = document.querySelector('.box')  //最外层的容器
            let imgs = document.querySelectorAll('img')  //要监听的元素
            const options = {
                root: box,    //外层元素
                threshold: 0.1   
            }
    
            function lazyload(target) {
                const io = new IntersectionObserver((entries, observer) => {
                    entries.forEach(entry => {
                        if (entry.isIntersecting) {
                            const img = entry.target
                            const src = img.getAttribute('data-src')
                            img.setAttribute('src', src)
                            img.classList.add('fade')
                            observer.disconnect()
                        }
                    })
                }, options)
                io.observe(target)
            }
            imgs.forEach(lazyload)
        </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    搭建yum服务器
    linux 网卡
    yum安装包另存
    CentOS下VMware用桥接模式,静态ip上外网
    linux挂载硬盘以及卸载硬盘
    Word2010如何编辑好了直接发布csdn博文?
    【更新】用word文档来发布到csdn等博客上边免去一张张上传图片的烦恼
    在word上写博客直接发到CSDN
    word上传博客教程
    Word写博客-使用Word2013发布博文到博客园
  • 原文地址:https://www.cnblogs.com/uimeigui/p/13914418.html
Copyright © 2011-2022 走看看