jQuery在页面中执行90%的操作

14ifxucb  于 2022-12-18  发布在  jQuery
关注(0)|答案(2)|浏览(133)

我有一个小脚本,允许我的用户接收新的项目,而无需重新加载页面或使用分页。
我有一个相当大的页脚与一些信息,目前这个脚本只加载时,用户到达页面底部。
有没有什么方法可以让它在用户浏览了页面的90%后,函数就启动了?
以下是我目前的脚本:

<script type="text/javascript">
    alreadyloading = false;
    start = 20;
    stop = 30;
     
    $(window).scroll(function() {
        if ($('body').height() <= ($(window).height() + $(window).scrollTop())) {
            if (alreadyloading == false) {
                alreadyloading = true;
                $('.last').show();
                $.ajax({
                    type: "GET",
                    url: "/trophy-room/all/",
                    data: "start="+start+"&stop="+stop,
                    success: function(data) {
                        $(data).insertBefore('.last');
                        start = stop;
                        stop = stop + 10;
                        $('.last').hide();
                        alreadyloading = false;
                    }
                });
                // if you reach bottom, send 10 as begging and 20 as stop
                // load the data and insert before div last or something. [5:10]
            }
        }
    });
</script>
eoigrqb6

eoigrqb61#

超级简单,$('body').height()乘以0.9。

<script type="text/javascript">
    alreadyloading = false;
    start = 20;
    stop = 30;

    $(window).scroll(function() {
        if (($(window).scrollTop + $(window).height()) >= ($('body').height() * 0.9)) {
            if (alreadyloading == false) {
                alreadyloading = true;
                $('.last').show();
                $.ajax({
                    type: "GET",
                    url: "/trophy-room/all/",
                    data: "start="+start+"&stop="+stop,
                    success: function(data) {
                        $(data).insertBefore('.last');
                        start = stop;
                        stop = stop + 10;
                        $('.last').hide();
                        alreadyloading = false;
                    }
                });
                // if you reach bottom, send 10 as begging and 20 as stop
                // load the data and insert before div last or something. [5:10]
            }
        }
    });
</script>
ne5o7dgx

ne5o7dgx2#

哦,比我早一点。但是这里有另一个解决方案。检查“prg”。

$(window).scroll(function() { 
a = document.documentElement.scrollTop;
b = document.body.scrollTop;
c = document.documentElement.scrollHeight;
d = document.documentElement.clientHeight;

prg = Math.round(((a+b) /(c-d))*100);

});

相关问题