zoukankan      html  css  js  c++  java
  • JS实现图片懒加载demo

    <!DOCTYPE html>
    <html>
    <head>
        <script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
        <title>demo lazyload</title>
        <meta charset="utf-8">
        <style type="text/css">
            * {
                padding: 0;
                margin: 0;
                text-decoration: none;
                list-style: none;
            }
            .layout {
                margin: 0 auto;
                width: 1000px;
            }
            .lazyload img {
                width: 300px;
                height: 400px;
            }
            .img-ct {
                margin-left: -50px;
                overflow: auto;
            }
            .item {
                float: left;
                margin-left: 50px;
                margin-bottom: 30px;
            }
        </style>
    </head>
    <body>
    <div class="lazyload">
      <div class="layout">
        <ul class="img-ct">
            <li class="item">
                <a href="javascript:void(0)"><img data-img="1.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="1.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="1.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="1.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="2.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="2.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="2.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="2.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="2.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="3.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="3.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="3.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="3.png" src="default.png"></a>
            </li>
            <li class="item">
                <a href="javascript:void(0)"><img data-img="3.png" src="default.png"></a>
            </li>
        </ul>
      </div>
    </div>
    
    <script type="text/javascript">
    var lazyLoad = (function(){
        var clock;
        
        function init(){
            $(window).on("scroll", function(){
                if (clock) {
                    clearTimeout(clock);
                }
                clock = setTimeout(function(){
                    checkShow();
                }, 200);
            })
            checkShow();
        }
        
        function checkShow(){
            $(".lazyload img").each(function(){
                var $cur =$(this);
                if($cur.attr('isLoaded')){
                    return;
                }
                if(shouldShow($cur)){
                    showImg($cur);
                }
            })
        }
    
        function shouldShow($node){
            var scrollH = $(window).scrollTop(),
                winH = $(window).height(),
                top = $node.offset().top;
    
            if(top < winH + scrollH){
                return true;
            }else{
                return false;
            }
        }
    
        function showImg($node){
            $node.attr('src', $node.attr('data-img'));
            $node.attr('isLoaded', true);
        }
    
        return {
            init: init
        }
    })()
    
    lazyLoad.init();
    </script>
    </body>
    </html>
  • 相关阅读:
    windows 挂载windows 共享盘为本地磁盘
    模块
    message 匹配不上grok正则 也会写入到elasticsearch
    【Java】No enclosing instance of type XXX is accessible. Must qualify the allocation with an enclosing instance of type XXX(e.g. x.new A() where x is an instance of XXX).的解决办法
    【Ubuntu】常用软件及其下载地址
    【Java】内存分析
    【Java】利用二维数组进行排版
    【Java】一维数组
    【趣题】用for循环输出菱形
    【趣题】穷举法解决百钱买百鸡
  • 原文地址:https://www.cnblogs.com/chenjiacheng/p/7232392.html
Copyright © 2011-2022 走看看