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>
    

      

  • 相关阅读:
    csr_matrix参数解析
    SQL删除重复数据(根据多个字段),pandas的nan存入数据库报错
    XGBoost参数中文翻译以及参数调优
    pandas.DataFrame.quantile
    pandas.DataFrame.rank
    JOIN子句
    ORDER BY子句
    WHERE子句
    SELECT语句
    数据分析-基础
  • 原文地址:https://www.cnblogs.com/uimeigui/p/13914418.html
Copyright © 2011-2022 走看看