评论:原生js图片懒加载插件  [查看原文]

所属分类:UI-加载,滚动

 34528  370  3
当前第1页 / 共1页
    怪咖0
    2018/9/30 9:38:31
    请问这个可以在 移动端使用吗?为什么一直报 viewHeight 未定义呢?
        1230
        2018/11/14 17:39:20
        js冲突
        -_Jasminee0
        2019/3/25 14:52:57
        window.onscroll = function () {
           Limg()
        }
        window.onload = function () {
           var img = document.querySelectorAll("img[data-src]")
           for (var i = 0; i < img.length; i++) {
               img[i].style.opacity = "0"
           }
           Limg()
        }
        
        function Limg() {
           var viewHeight = document.documentElement.clientHeight
           var t = document.documentElement.scrollTop || document.body.scrollTop;
           var limg = document.querySelectorAll("img[data-src]")
           Array.prototype.forEach.call(limg, function (item, index) {
               var rect
               if (item.getAttribute("data-src") === "")
                   return
               rect = item.getBoundingClientRect()
               if (rect.bottom >= 0 && rect.top < viewHeight) {
                   (function () {
                       var img = new Image()
                       img.src = item.getAttribute("data-src")
                       item.src = img.src
                       var j = 0
                       setInterval(function () {
                           j += 0.2
                           if (j <= 1) {
                               item.style.opacity = j
                               return
                           }
                       }, 100)
                       item.removeAttribute('data-src')
                   })()
               }
           })
        }
    回复

讨论这个项目(3)回答他人问题或分享插件使用方法奖励jQ币 评论用户自律公约

取消回复