• 【原生js实现---图片懒加载代码】


    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <title>图片懒加载(可视区域加载)</title>
    <style>
    * {
    padding: 0px;
    margin: 0px;
    }

    html,
    body {
    100%;
    min-height: 100%;
    }

    #SB {
    margin: 0;
    padding: 0;
    list-style: none;
    }

    #SB .in {
    border: 1px solid red;
    margin: 10px;
    text-align: center;
    height: 400px;
    400px;
    float: left;
    }

    .in img {
    border: none;
    vertical-align: middle;
    height: 400px;
    400px;
    }
    </style>
    </head>

    <body>
    <ul id="SB">
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>
    <li class="in"><img src="" data-imgurl="img/show.jpg" alt="loading"></li>

    </ul>
    <script type="text/javascript">
    var aImages = document.getElementById("SB").getElementsByTagName('img'); //获取id为SB的文档内所有的图片
    loadImg(aImages);
    window.onscroll = function() { //滚动条滚动触发
    loadImg(aImages);
    };
    //getBoundingClientRect 是图片懒加载的核心
    function loadImg(arr) {
    for(var i = 0, len = arr.length; i < len; i++) {
    if(arr[i].getBoundingClientRect().top < document.documentElement.clientHeight && !arr[i].isLoad) {
    arr[i].isLoad = true; //图片显示标志位
    //arr[i].style.cssText = "opacity: 0;";
    (function(i) {
    setTimeout(function() {
    if(arr[i].dataset) { //兼容不支持data的浏览器
    aftLoadImg(arr[i], arr[i].dataset.imgurl);
    } else {
    aftLoadImg(arr[i], arr[i].getAttribute("data-imgurl"));
    }
    arr[i].style.cssText = "transition: 1s; opacity: 1;" //相当于fadein
    }, 500)
    })(i);
    }
    }
    }

    function aftLoadImg(obj, url) {
    var oImg = new Image();
    oImg.onload = function() {
    obj.src = oImg.src; //下载完成后将该图片赋给目标obj目标对象
    }
    oImg.src = url; //oImg对象先下载该图像
    }
    </script>
    </body>

    </html>

    《敢想敢做,就敢于去实现它》 -----我的座右铭
  • 相关阅读:
    【codevs1922】骑士共存问题——网络流
    【bzoj1855||hdu3401】股票交易——单调队列优化dp
    【bzoj2002】弹飞绵羊——分块
    【bzoj3790】神奇项链——manacher+贪心
    【hdu2149】Public Sale
    【hdu1846】Brave Game
    【hdu3294】Girls' research——manacher
    【hdu3068】最长回文——manacher算法
    [BZOJ] 1634: [Usaco2007 Jan]Protecting the Flowers 护花
    [BZOJ] 1651: [Usaco2006 Feb]Stall Reservations 专用牛棚
  • 原文地址:https://www.cnblogs.com/chenhongshuang/p/8779296.html
Copyright © 2020-2023  润新知