1

我有这个代码

// If there is no data returned, there are no more posts to be shown. Show error
if(data == "") { 

    $this.find('.loading-bar').html($settings.error);                   

} else {

    // Offset increases
    offset = offset+$settings.nop; 

    // Append the data to the content div
    $this.find('#content_ins_con_all_posts').append(data);

    // No longer busy!  
    busy = false;
}

此代码在点击底部时向我显示一条消息,并且没有其他帖子可显示。我的问题是,当我继续滚动时,消息会一次又一次地多次显示...我只想在帖子结束时向我显示一次消息。如果有办法我可以做到这一点,这是非常受欢迎的。

(function($) {

$.fn.scrollPagination = function(options) {

    var settings = { 
        nop     : 3, // The number of posts per scroll to be loaded
        offset  : 0, // Initial offset, begins at 0 in this case
        error   : 'No More Posts!', // When the user reaches the end this is the message that is
                                    // displayed. You can change this if you want.
        delay   : 2000, // When you scroll down the posts will load after a delayed amount of time.
                       // This is mainly for usability concerns. You can alter this as you see fit
        scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
                       // but will still load if the user clicks.
    }

    // Extend the options so they work with the plugin
    if(options) {
        $.extend(settings, options);
    }

    // For each so that we keep chainability.
    return this.each(function() {       

        // Some variables 
        $this = $(this);
        $settings = settings;
        var offset = $settings.offset;
        var busy = false; // Checks if the scroll action is happening 
                          // so we don't run it multiple times

        // Custom messages based on settings
        if($settings.scroll == true) $initmessage = 'Scroll for more or click here';
        else $initmessage = 'Click for more';

        // Append custom messages and extra UI
        $this.append('<div id="content_ins_con_all_posts"></div><div class="loading-bar">'+$initmessage+'</div>');

        function getData() {

            // Post data to ajax.php
            $.post('functions_index_result_all_img.php', {

                action        : 'scrollpagination',
                number        : $settings.nop,
                offset        : offset,

            }, function(data) {

                // Change loading bar content (it may have been altered)
                $this.find('.loading-bar').html($initmessage);

                // If there is no data returned, there are no more posts to be shown. Show error

                if(data == "") { 
                    $this.find('.loading-bar').html($settings.error);                   

                } 
                else {  
                    // Offset increases
                    offset = offset+$settings.nop; 

                    // Append the data to the content div
                    $this.find('#content_ins_con_all_posts').append(data);

                    // No longer busy!  
                    busy = false;

                }

            });

        }   

        getData(); // Run function initially

        // If scrolling is enabled
        if($settings.scroll == true) {
            // .. and the user is scrolling
            $(window).scroll(function() {

                // Check the user is at the bottom of the element
                if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {

                    // Now we are working, so busy is true
                    busy = true;

                    // Tell the user we're loading posts
                    $this.find('.loading-bar').html('Loading Posts');

                    // Run the function to fetch the data inside a delay
                    // This is useful if you have content in a footer you
                    // want the user to see.
                    setTimeout(function() {

                        getData();

                    }, $settings.delay);

                }   
            });
        }

        // Also content can be loaded by clicking the loading bar/
        $this.find('.loading-bar').click(function() {

            if(busy == false) {
                busy = true;
                getData();
            }

        });

    });
}

})(jQuery);
4

3 回答 3

3

两种方式离我的头顶

  1. 检查光标在哪里,如果它在距离底部的某个范围附近,则不要触发该代码
  2. 将标志存储在 sessionStorage 或变量中(取决于您的代码),这意味着您已经显示了消息,一旦光标从底部移开超过您定义的一定距离,就删除标志。
于 2013-07-18T09:51:59.277 回答
1

您可以使用全局变量来了解是否必须显示消息

像这样的东西:

var isDisplayed = false;

然后

if (isDisplayed == false)
{
   isDisplayed = true;
   displayMessage();
}
于 2013-07-18T09:55:51.033 回答
1

您可以在代码中设置标志。就像是:

// If there is no data returned, there are no more posts to be shown. Show error
if(data == "") { 

    if(!flag) {
        $this.find('.loading-bar').html($settings.error);
        flag = true; // You need to declare flag = false at an appropriate place in your code.
    }
} else {

    // Offset increases
    offset = offset+$settings.nop; 

    // Append the data to the content div
    $this.find('#content_ins_con_all_posts').append(data);

    // No longer busy!  
    busy = false;

}

请以此为起点,而不是作为复制粘贴解决方案。

于 2013-07-18T09:54:37.867 回答