码迷,mamicode.com
首页 > 其他好文 > 详细

图片延迟加载

时间:2018-03-25 20:46:41      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:name   block   idt   延迟加载   oct   app   hidden   一个   head   

<!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>

图片延迟加载

标签:name   block   idt   延迟加载   oct   app   hidden   一个   head   

原文地址:https://www.cnblogs.com/xiaobaizitaibai/p/8646362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!