• 图片懒加载的实现


    <!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>
    

      

  • 相关阅读:
    hdu 5253 最小生成树
    hdu5248 序列变换
    bjfu1299 stl使用
    bjfu1277 简单递归
    bjfu1262 优先队列
    bjfu1287字符串输出的大水题
    bjfu1281
    bjfu1253 最大上升子序列和
    [转][Unity3D]引擎崩溃、异常、警告、BUG与提示总结及解决方法
    Unity3d 中 将远程 MySQL 数据库转换为本地 Sqlite
  • 原文地址:https://www.cnblogs.com/uimeigui/p/13914418.html
Copyright © 2020-2023  润新知