js 手机端滑动分页SPager 基本示例

HTML页

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, width=device-width,minimum-scale=1.0,maximum-scale=1.0, user-scalable=no" />
    <title>SPager</title>
    <link href="SPager.css" rel="stylesheet" />
    <script src="SPager.js"></script>
</head>
<body>
    
</body>
</html>

CSS页

*{
    margin:0px;
    padding:0px;
}
body{
    
}
div{
    height:80px;
}
#loading{
    height:66px;
}


JavaScript页

(function () {
    window.SPager = function () {
        window._this = this;
        this.body = document.body;
        this.W = window.innerWidth;
        this.H = window.innerHeight;
        this.cc = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
        this.PageSize = 10;
        this.count=0;
        this.loadgif = null;
        this.img_loading_flag = false;
    };
    SPager.prototype = {
        Start: function () {
            this.loadimg();
            this.Create_div();
            this.AddEvent();
        },
        loadimg:function(){
            this.loadgif = new Image();
            this.loadgif.onload = function(){
                _this.img_loading_flag=true;
            };
            this.loadgif.src = "imgs/loading.gif";
        },
        Create_div: function () {
            for (var i = 0; i < this.PageSize; i++) {
                var div = document.createElement("div");
                div.setAttribute("style", "width:" + this.W);
                div.setAttribute("style", "background-color:" + this.getColor());
                this.count++;
                div.innerText = this.count;
                document.body.appendChild(div);
            }
        },
        getColor: function () {
            var divcolor = "#";
            for (var i = 0; i < 6; i++) {
                var ran = Math.floor(16 * Math.random());
                divcolor += this.cc[ran];
            }
            return divcolor;
        },
        AddEvent: function () {
            var fun = function () {
                if (document.body.scrollTop == document.body.scrollHeight - _this.H) {
                    document.body.removeEventListener("touchmove", fun, false);
                    _this.AddLoading();
                    setTimeout(function () {
                        _this.RemoveLoading();
                        _this.Create_div();
                        document.body.addEventListener("touchmove", fun, false);
                    }, 3000);
                }   
            };
            document.body.addEventListener("touchmove", fun, false);
        },
        AddLoading: function () {
            var loading = document.createElement("div");
            loading.setAttribute("id", "loading");
            loading.setAttribute("style", "width:" + this.W);
            loading.setAttribute("style", "text-align:center");
            while(!this.img_loading_flag){

            }
            loading.appendChild(this.loadgif);
            document.body.appendChild(loading);
        },
        RemoveLoading: function () {
            var loading = document.getElementById("loading");
            document.body.removeChild(loading);
        },
    };
}());
document.addEventListener("DOMContentLoaded", function () { new SPager().Start() }, false);



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值