重新检查 jQuery 中的某些内容?

发布于 2024-10-01 03:26:36 字数 858 浏览 1 评论 0原文

基本上我有一个 jQuery 函数来检查用户是否位于页面底部附近并加载更多内容。当文档加载时,它会运行以进行检查。

<script type="text/javascript">
$(document).ready(function() {

    var number = 5;
    offset = 0;

    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() > $(document).height() - 10) {

            $(".empty-div").append().load('<?php bloginfo('template_url'); ?>/ajax.php?offset='+offset+number, function() {

                    var offset = offset+number;             

                    setTimeout(function(){ console.log('after', $(document).height()); }, 0);
                    setTimeout(function(){ console.log('after', $(window).height()); }, 0);



            });     

        }

    });

});
</script>

每当我第二次到达页面底部时,它都不会重做加载功能。顺便说一句,第一次确实有效。有什么想法可以解决这个问题吗?控制台的东西是用来重新计算高度的,以防万一这就是导致它的原因。

Basically I have a jQuery function that checks if the user is near the bottom of the page and loads more stuff. When the document loads this is ran, to check.

<script type="text/javascript">
$(document).ready(function() {

    var number = 5;
    offset = 0;

    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() > $(document).height() - 10) {

            $(".empty-div").append().load('<?php bloginfo('template_url'); ?>/ajax.php?offset='+offset+number, function() {

                    var offset = offset+number;             

                    setTimeout(function(){ console.log('after', $(document).height()); }, 0);
                    setTimeout(function(){ console.log('after', $(window).height()); }, 0);



            });     

        }

    });

});
</script>

H/e whenever I reach the bottom of the page on the second go, it doesn't redo the load function. It does work the first time, by the way. Any ideas how to remedy this? The console stuff is there to recalculate the heights, just in case that's what's causing it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-10-08 03:26:36

通过这样做:

var offset = offset+number; 

您正在创建一个本地offset变量,而不是更新

offset = 0;

函数之前声明的原始变量,因此:?offset='+offset+ number 始终使用 offset 作为 0,因为它从未更新。只需删除 var,如下所示:

offset = offset+number; 

为了简洁起见,还可以在原始声明中添加 var,或者使用逗号进行多个声明,如下所示:

var number = 5, offset = 0;

By doing this:

var offset = offset+number; 

You're creating a new local offset variable, not updating your original

offset = 0;

declared before the function, so this: ?offset='+offset+number is always using offset as 0, since it's never updated. Just remove the var, like this:

offset = offset+number; 

For cleanliness, also add a var to the original declaration, or use a comma for a multiple declaration, like this:

var number = 5, offset = 0;
亚希 2024-10-08 03:26:36
  1. 你在 $(".empty-div") 上使用 load() ,这不会添加更多内容,它将替换内容(如果响应没有改变,你将看不到任何变化)

    最好应该是这样的

    $('
    ').appendTo(".empty-div").load(/*****/)
  2. 的东西,也许 DOCTYPE 也有问题。日志

    $(window).scrollTop()、$(window).height() 和 $(document).height() 
    

    也许,标准模式和怪异模式之间有所不同。

  1. you use load() there on $(".empty-div"), this would'nt add more content, it will replace the content(if the response does'nt change, you will see no change)

    It better should be something like

    $('<div/>').appendTo(".empty-div").load(/*****/)
    
  2. maybe there also is a problem with the DOCTYPE. Log

    $(window).scrollTop(),  $(window).height() and  $(document).height() 
    

    maybe, it differs between standards - and quirks-mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文