sailei1 / blog

1 stars 0 forks source link

JS 下拉加载原理 #47

Closed sailei1 closed 5 years ago

sailei1 commented 5 years ago
<!DOCTYPE html>
<html>
<head>
    <title>无限翻页测试</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style type="text/css">
    #spinner{
        position: fixed;
        top: 20px;
        left: 40%;
        display: block;
        color: red;
        font-weight: 900;
        background-color: rgba(80, 80, 90, 0.22);
        padding-top: 20px;
        padding-bottom: 20px;
        padding-left: 100px;
        padding-right: 100px;
        border-radius: 15px;
    }
    </style>
</head>
<body>
    <div id="sample">
    </div>
    <div id="spinner">
        正在加载
    </div>
    <script type="text/javascript">
        var index = 0;

// scrollHeight即真实内容的高度;
// clientHeight比较好理解,是视窗的高度,就是我们在浏览器中所能看到内容的高度;
//scrollTop是视窗上面隐藏掉的部分。
        function lowEnough(){
            var pageHeight = Math.max(document.body.scrollHeight,document.body.offsetHeight);
            var viewportHeight = window.innerHeight || 
                document.documentElement.clientHeight ||
                document.body.clientHeight || 0;
            var scrollHeight = window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop || 0;
            // console.log(pageHeight);
            // console.log(viewportHeight);
            // console.log(scrollHeight);
            return pageHeight - viewportHeight - scrollHeight < 20; //页面高度-窗口高度- 头部隐藏高度
        }

        function doSomething(){
            var htmlStr = "";
            for(var i=0;i<10;i++){
                htmlStr += "这是第"+index+"次加载<br>";
            }
            $('#sample').append(htmlStr);
            index++;
            pollScroll();//继续循环
            $('#spinner').hide();
        }

        function checkScroll(){
            if(!lowEnough()) return pollScroll();

            $('#spinner').show();
            setTimeout(doSomething,900);

        }
        function pollScroll(){
            setTimeout(checkScroll,1000);
        }
        checkScroll();
    </script>
</body>
</html>
sailei1 commented 5 years ago
 scrollTarget(event){
                setTimeout(()=> {
                    let target = event.target;
                    if (this.lock) return;
                    if (target.clientHeight + target.scrollTop > target.scrollHeight - 800) {
                        this.lock = true;
                        let index = this.start;

                        if (index >= this.comps.length) {
                            return;
                        }
                        let item = this.comps[index];
                        item.show = true;

                        // this.comps=Object.assign(t,this.comps);
                        Vue.set(this.comps, index, item);
                        setTimeout(() => {
                            this.lock = false;
                            this.start++;
                        }, 0)

                    }
                },0);
            },