美文网首页前端开发
前端优化之lazy-load

前端优化之lazy-load

作者: Chris__Liu | 来源:发表于2019-03-05 21:44 被阅读0次

页面加载性能问题

最近我的BOSS说发现个问题,老项目打开的时候特别的卡,我仔细的研究了一下页面的结构,发现这个页面有很多图片要加载,我想到的方案就是用懒加载来实现性能的提升。

实现思路

  • 第一种是计算每张图片相对屏幕顶端的距离就是offsetTop,如果offsetTop小于等于滚动的距离就是document.body.scrollTop和视口高度window.innerHeight之和,这个图片就会加载。
  • 第二种是运用API IntersectionObserver,去动态检测元素是否出现的屏幕内,jquery_lazyLoad也支持了此API。

JS方案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>LozyLoad</title>
    <style>
        .images{
            display: flex;
            flex-direction: column;
            text-align: center;
            width: 500px;
        }
        .img-item{
            height:400px;
            width: 400px;
            margin: 20px;
        }

    </style>
</head>
<body>
    <div class="images">
        <img class="img-item" alt="loading" data-src="./img/1.png">
        <img class="img-item" alt="loading" data-src="./img/2.png">
        <img class="img-item" alt="loading" data-src="./img/3.png">
        <img class="img-item" alt="loading" data-src="./img/4.png">
        <img class="img-item" alt="loading" data-src="./img/5.png">
    </div>
    <script type="text/javascript">
        var imgs = document.querySelectorAll('img');
        var lazyload = function(){
            var scrollTop = document.body.scrollTop || document.documentElement.scrollTop
            var winTop = window.innerHeight
            for(var i=0;i < imgs.length;i++){
                if(imgs[i].offsetTop <= scrollTop + winTop ){
                    imgs[i].src = imgs[i].getAttribute('data-src')
                }
            }
        }
        function throttle(method,delay){
            let timer = null
            return function(){
                let args = arguments
                clearTimeout(timer)
                timer = setTimeout(function(){
                    console.log(this)
                    method.apply(this.args)
                },delay)
            }
        }
        lazyload()
        window.onscroll = throttle(lazyload,200)
    </script>

    <script type="text/javascript">
        //获取观察器实例  changes是被观察的对象数组
        var observer = new IntersectionObserver(function(changes){  
            console.log(changes);
            changes.forEach(function(index,item){
                if(item.intersectionRatio > 0 && item.intersectionRatio < 1)
                    //target:被观察的目标元素,是一个 DOM 节点对象
                    item.target.src = item.target.dataset.src;
            });
        });
        function addObserver(){
            var listItems = document.querySelectorAll('.img-item');
            listItems.forEach(function(item){
                //实例的observe方法可以指定观察哪个DOM节点
                //开始观察  observe的参数是一个 DOM 节点对象
                observer.observe(item);
            });
        }
        addObserver();
    </script>
    
</body>
</html>

Jquery方案

jquery_lazyload

Vue

vue-lazyload
vue-lazyload原理解析

React

react-lazyload

相关文章

  • 前端优化之lazy-load

    页面加载性能问题 最近我的BOSS说发现个问题,老项目打开的时候特别的卡,我仔细的研究了一下页面的结构,发现这个页...

  • 前端性能优化(中)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(上)...

  • 前端性能优化(下)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 《前端性能优化(中)...

  • Chrome 性能监控 性能分析

    Chrome开发者工具之JavaScript内存分析 前端性能优化 —— 前端性能分析 Chrome DevToo...

  • 前端性能优化(上)

    性能优化调研系列文章 《前端性能优化(上)》 《前端性能优化(中)》 《前端性能优化(下)》 为什么要进行前端性能...

  • 冒泡排序(ios和前端script)

    ios之冒泡排序 未优化之前 优化之后 前端冒泡排序(与上同理) 方式一: 方式二:

  • Android webview-tips

    webview优化 Android学习之 WebView使用小结 Android 各个版本WebView 移动前端...

  • 手摸手教你实现图片懒加载

    懒加载(Lazy-Load)。它是针对图片加载时机的优化:在一些图片量比较大的网站(比如电商网站首页,或者团购网站...

  • 前端性能优化之Lazyload

    前端性能优化之Lazyload @(冬晨)[JavaScript|技术分享|懒加载] [TOC] Lazyload...

  • 前端性能优化

    js性能小贴士——优化循环 前端网页与js性能优化 我总结的js性能优化的小知识 提高 web 应用性能之 Jav...

网友评论

    本文标题:前端优化之lazy-load

    本文链接:https://www.haomeiwen.com/subject/lofkuqtx.html