前端实现懒加载

<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
    <style type="text/css">
        .page-scroll-container>div &#123;
            position: relative;
            margin-bottom: 20px
        &#125;

        .page-scroll-container a &#123;
            overflow: hidden;
            display: block
        &#125;

        .mask &#123;
            background-color: rgba(0, 0, 0, 0.6);
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            filter: alpha(opacity=0);
            transition: all .5s;
            -webkit-transition: all .5s;
            -moz-transition: all .5s;
            -o-transition: all .5s;
            -ms-transition: all .5s;
            text-align: center;
            color: #ffffff
        &#125;

        .mask h4 &#123;
            transform: translateY(10px);
            opacity: 0.5;
            transition-duration: 0.8s;
            transition-delay: .2s;
            margin-top: 28%;
            font-size: 36px;
            line-height: 42px
        &#125;

        .page-scroll-container>div:hover .mask h4 &#123;
            opacity: 1;
            transform: translateY(-10px)
        &#125;

        .mask strong &#123;
            transform: translateY(10px);
            opacity: 0.8;
            transition-duration: 1s;
            transition-delay: .3s;
            display: block
        &#125;

        .page-scroll-container>div:hover .mask strong &#123;
            transform: translateY(-10px);
            opacity: 1;
            transition-duration: opacity 1s
        &#125;

        .mask span &#123;
            color: #fff;
            transform: translateY(10px);
            opacity: 0.5;
            transition-duration: .8s;
            transition-delay: .4s;
            display: block;
            font-size: 14px
        &#125;

        .page-scroll-container>div:hover span &#123;
            opacity: 1;
            transform: translateY(-10px)
        &#125;

        .page-scroll-container>div:hover .mask &#123;
            opacity: 1;
            filter: alpha(opacity=100);
            transform: scale(1)
        &#125;

        .page-scroll-container img &#123;
            width: 100%;
            height: 100%;
            transition: all .5s;
            -webkit-transition: all .5s;
            -moz-transition: all .5s;
            -o-transition: all .5s;
            -ms-transition: all .5s;
            overflow: hidden
        &#125;

        .page-scroll-container a:hover img &#123;
            -webkit-transform: scale(1.2);
            -moz-transform: scale(1.2);
            -o-transform: scale(1.2);
            -ms-transform: scale(1.2);
            transform: scale(1.2)
        &#125;

        .more &#123;
            width: 280px;
            height: 55px;
            line-height: 55px;
            background: #d7000f;
            margin: 20px auto 50px auto;
            text-align: center;
            border-radius: 26px
        &#125;

        .more a &#123;
            color: #ffffff;
            font-size: 18px
        &#125;
    </style>
</head>

<body>

    <div class="page-scroll-container row"></div>
    <div class="more">
        <a id="itemMore">查看更多造势案例+</a>
    </div>
    <script src="./js/jquery.min.js"></script>
    <script>
        (function ($, document) &#123;
            /*前端实现-懒加载插件pageScroll
             * param el       &#123;object&#125; jquery对象,默认: $('.page-scroll-container')
             * param datas    &#123;array &#125; 数据数组,格式:[&#123;img:'',title:'',content:''&#125;,...],默认:示例
             * param pageSize &#123;number&#125; 每页显示数量,默认: 6
             * param currentPage &#123;number&#125; 当前页,默认: 0
             * param addButton &#123;object&#125; jquery对象,默认: $('#itemMore')
             * param bottom &#123;number&#125; 距底部距离,默认: 0
             * param loadingTime &#123;number&#125; 加载时间,默认: 200ms
             * */
            $.pageScroll = function (option) &#123;
                option = option || &#123;&#125;;
                var defaultOption = &#123;
                    el: $('.page-scroll-container'),
                    arr: option.datas || JSON.parse(('[' + new String(
                            ',&#123;"img":"img/index3_1.png","title":"Brand","content":"内容"&#125;').repeat(20)
                        .substring(1) + ']')),
                    pagesize: option.pageSize || 6,
                    currentPage: option.currentPage || 0,
                    $loadText: option.addButton || $('#itemMore'),
                    loadTxt: &#123;
                        more: '查看更多造势案例+',
                        loading: '<span>加载中...</span>',
                        end: '没有更多了'
                    &#125;,
                    bottom: option.bottom || 0,
                    loadingTime: option.loadingTime || 200,
                    isloading: false
                &#125;;
                Object.defineProperty(defaultOption, "page", &#123;
                    get: function () &#123;
                        if (option.page) &#123;
                            return option.page
                        &#125;
                        return this.arr.length % this.pagesize ? (parseInt(this.arr.length / this
                            .pagesize) + 1) : this.arr.length / this.pagesize;
                    &#125;
                &#125;);
                defaultOption.toPage = function () &#123;
                    var that = this;
                    if (!this.isloading) &#123;
                        this.isloading = true;
                        var res = [];
                        for (var i = this.currentPage * this.pagesize, len = ((i + this.pagesize < this.arr
                                .length) ? i + this.pagesize : this.arr.length); i < len; i++) &#123;
                            res.push(
                                '<div class="imgitem col-md-4 col-sm-4 col-xs-6" style="display:none;">'
                                );
                            res.push('	<a href="#">');
                            res.push('		<img src="' + this.arr[i].img + '" class="img-responsive">');
                            res.push('		<div class="mask">');
                            res.push('			<h4>' + this.arr[i].title + '</h4>');
                            res.push('			<strong>' + this.arr[i].content + '</strong>');
                            res.push('			<span class="qwe">查看该案例详情</span>');
                            res.push('		</div>');
                            res.push('	</a>');
                            res.push('</div>');
                        &#125;
                        $(this.el).append(res.join(''));
                        $('.imgitem:not(:visible)').fadeIn(1500);
                        this.$loadText.html(this.loadTxt.more);
                        this.currentPage++;
                        setTimeout(function () &#123;
                            that.isloading = false;
                        &#125;, 1000);
                    &#125;
                &#125;;
                defaultOption.init = function () &#123;
                    var that = this;
                    this.toPage();
                    //点击展开更多
                    this.$loadText.on('click', function () &#123;
                        if (that.currentPage < that.page) &#123;
                            that.$loadText.html(that.loadTxt.loading);
                            setTimeout(function () &#123;
                                that.toPage();
                            &#125;, that.loadingTime);
                        &#125; else &#123;
                            that.$loadText.html(that.loadTxt.end);
                        &#125;
                        that.toPage();
                        return false;
                    &#125;);
                    //页面滚动到距底部x距离时展开更多
                    $(window).on('scroll', function () &#123;
                        var wHeight = $(window).innerHeight();
                        var dHeight = $(document).height();
                        var sHeight = $(document).scrollTop();
                        var bottomLength = dHeight - wHeight - sHeight;
                        if (bottomLength <= that.bottom) &#123; //距底部高度
                            if (that.currentPage < that.page) &#123;
                                that.$loadText.html(that.loadTxt.loading);
                                setTimeout(function () &#123;
                                    that.toPage();
                                &#125;, that.loadingTime);
                            &#125; else &#123;
                                that.$loadText.html(that.loadTxt.end);
                            &#125;
                        &#125;
                    &#125;);
                &#125;
                defaultOption.init();
                return defaultOption;
            &#125;
        &#125;)(jQuery, document);
        //调用方式
        $.pageScroll();
    </script>
</body>

</html>

示例:请点这里


  转载请注明: XMwarrior 前端实现懒加载

 上一篇
canvas转换svg合成图片 canvas转换svg合成图片
此函数应用场景为:百度地图生成自定义标志 (function ($, document) &#123; $.extend(&#123; //16进制颜色转换为rgba颜色 colorR
2019-02-01
下一篇 
js规范 js规范
注释原则As short as possible(如无必要,勿增注释):尽量提高代码本身的清晰性、可读性。 As long as necessary(如有必要,尽量详尽):合理的注释、空行排版等,可以让代码更易阅读、更具美感。 单行注释必须
2018-04-13
  目录