图片延迟加载

Posted 小白字太白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片延迟加载相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        * {margin:0; padding:0;}
        ul {list-style:none;}
        .wrapper {width:1000px; margin:100px auto 0;}
        .wrapper h2 {background:#ccc; height:40px; line-height:40px; text-align:center;}

        .box {border:1px solid #ccc; height:550px; margin:10px 0;}
        
        .wrapper ul {overflow:hidden; border:1px solid #ccc; padding:15px 0;}
        .wrapper li {float:left; width:199px;text-align:center;}
        .wrapper li a {display:block;}
    </style>
    <script type="text/javascript">
        window.onload = function(){
            
            //获取所有的图片
            var imgs = document.getElementsByTagName(‘img‘);

            //绑定一个滚动事件
            document.onscroll = function(){
                
                var st = document.body.scrollTop || document.documentElement.scrollTop;
                //获取当前视窗的高度,这个是不变的
                var ch = document.documentElement.clientHeight;
                for (var i = 0 , len = imgs.length; i < len; i++) {
                    var ot = imgs[i].offsetTop;
                    if (ch + st >= ot + 50) {
                        imgs[i].src = imgs[i].getAttribute(‘data-src‘);
                    }

                }
                /*
                var ot = imgs[0].offsetTop;
                console.log(st);
                console.log( imgs[0].offsetTop );
                console.log( ch );
                //此处加50只是为了测试,实际开发中不需要
                if ( ch + st >= ot + 50) {
                    //图片应该要显示,就是设置其src属性
                    imgs[0].src = imgs[0].getAttribute(‘data-src‘);
                }*/

            }
        }
    
    </script>
</head>
<body>
    
    <div class="wrapper">
        <h2>图片延迟加载</h2>
        <div class="box">
        </div>  
        <ul>
            <li>
                <img data-src="images/v1.jpg" />
                <a href="">电影1</a>
            </li>
            <li>
                <img data-src="images/v2.jpg" />
                <a href="">电影2</a>
            </li>
            <li>
                <img data-src="images/v3.jpg" />
                <a href="">电影3</a>
            </li>
            <li>
                <img data-src="images/v4.jpg" />
                <a href="">电影4</a>
            </li>
            <li>
                <img data-src="images/v5.jpg" />
                <a href="">电影5</a>
            </li>
        </ul>

        <div class="box">
        
        </div>
        <ul>
            <li>
                <img data-src="images/v6.jpg" />
                <a href="">电影6</a>
            </li>
            <li>
                <img data-src="images/v7.jpg" />
                <a href="">电影7</a>
            </li>
            <li>
                <img data-src="images/v8.jpg" />
                <a href="">电影8</a>
            </li>
            <li>
                <img data-src="images/v9.jpg" />
                <a href="">电影9</a>
            </li>
            <li>
                <img data-src="images/v10.jpg" />
                <a href="">电影10</a>
            </li>
        </ul>

        <div class="box">
        
        </div>
        <ul>
            <li>
                <img data-src="images/v11.jpg" />
                <a href="">电影11</a>
            </li>
            <li>
                <img data-src="images/v12.jpg" />
                <a href="">电影12</a>
            </li>
            <li>
                <img data-src="images/v13.jpg" />
                <a href="">电影13</a>
            </li>
            <li>
                <img data-src="images/v14.jpg" />
                <a href="">电影14</a>
            </li>
            <li>
                <img data-src="images/v15.jpg" />
                <a href="">电影15</a>
            </li>
        </ul>
        <div class="box">
        
        </div>
    </div>
    

</body>
</html>

以上是关于图片延迟加载的主要内容,如果未能解决你的问题,请参考以下文章

延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)

延迟加载缓冲图片

延迟加载

js学习总结----延迟加载思想和首屏延迟加载

图片延迟加载

图片延迟加载的实现