• javascript 图片延迟加载


    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <style>
            #div1 {
                height: 200px;
                background-color: #bbbbbb;
                font-size: 100px;
            }
        </style>
    </head>
    <body style="height:3500px">
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br /><br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br /><br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <img id="div1" src="../Img/4.png" realsrc="http://cdn1.gurucv.com/Reg/Images/person/person1.png" />
    
        <script type="text/javascript">
            //var ele = document.getElementById('div1');
            var ele = document.getElementsByTagName('img').item(0);
            function offset(ele) {
                var t = ele.offsetTop;
                var l = ele.offsetLeft;
                var p = ele.offsetParent;
                while (p) {
                    if (window.navigator.userAgent.indexOf('MSIE 8') > -1) {
                        l += p.offsetLeft;
                        t += p.offsetTop;
                    } else {
                        l += p.offsetLeft + p.clientLeft;
                        t += p.offsetTop + p.clientTop;;
                    }
                    p = p.offsetParent;
                }
                return { left: l, top: t };
            }
            var h = offset(ele).top + ele.offsetHeight;//这是这个元素底边距文档顶部的绝对偏移量
            window.onscroll = function () {
                var winBottom = (document.documentElement.scrollTop || document.body.scrollTop) + (document.documentElement.clientHeight || document.body.clientHeight);
                if (h < winBottom) {
                    // ele.src = ele.realsrc; //自定义属性,不能用 ‘.’的方式去取
                    if (!ele.loaded) {
                        var realImg = ele.getAttribute('realsrc');
                        ele.src = '../Img/loading.jpg';
    
                        var tempImg = new Image;//等价于 document.createElement('img');
                        tempImg.src = realImg;//自动下载图片,自动去请求图片
                        /*
                        浏览器所有的外链资源,都是缓存
                        */
                        tempImg.onload = function () {//只是被运行一次
                            ele.src = realImg;
                            ele.loaded = true;//标志属性
                        };
                    }
                }
            }
        </script>
    </body>>
    </html>
    

      

  • 相关阅读:
    慢性肾炎患者的家庭养护
    中国居民五谷膳食指南
    第二单元丨对环境的察觉
    一个人是否靠谱,闭环很重要
    打赢营销胜仗
    治本修身──中醫內分泌調節養生法
    理想的激励
    卓越演讲:打动听众必知必会的策略和方法(原书第3版)
    每日一题_191008
    每日一题_191007
  • 原文地址:https://www.cnblogs.com/alphafly/p/4690101.html
Copyright © 2020-2023  润新知