【问题标题】:Detect div scroll jquery检测div滚动jquery
【发布时间】:2014-02-08 07:28:42
【问题描述】:

我想检测 div 滚动。这段代码所做的是检测整个窗口滚动:

$(document).ready(function() {
var track_load = 0; //total loaded record group(s)
var loading  = false; //to prevents multipal ajax loads
var total_groups = <?php echo $total_groups; ?>; //total record group(s)

$('#results').load("autoload_process.php", {'group_no':track_load}, function() {track_load++;}); //load first group

$(window).scroll(function() { //detect page scroll

    if($(window).scrollTop() + $(window).height() == $(document).height())  //user scrolled to bottom of the page?
    {

        if(track_load <= total_groups && loading==false) //there's more data to load
        {
            loading = true; //prevent further ajax loading
            $('.animation_image').show(); //show loading image

            //load data from the server using a HTTP POST request
            $.post('autoload_process.php',{'group_no': track_load},    function(data){

                $("#results").append(data); //append received data into the element

                //hide loading image
                $('.animation_image').hide(); //hide loading image once data is received

                track_load++; //loaded group increment
                loading = false; 

            }).fail(function(xhr, ajaxOptions, thrownError) { //any errors?

                alert(thrownError); //alert with HTTP error
                $('.animation_image').hide(); //hide loading image
                loading = false;

            });

        }
    }
});

});

这是我的 HTML 代码。

<div id="scrollingbox">
<ol id="results">
</ol>
<div class="animation_image" style="display:none" align="center"><img src="ajax-loader.gif"></div>
</div>

我使用了 div ID,但它什么都不输出,而不是两个。

【问题讨论】:

    标签: javascript jquery content-management-system scroll


    【解决方案1】:

    不确定,但您可以在 scroll() 函数中通过 id、类等引用 div:

    这是一个简单的 jsfiddle:http://jsfiddle.net/collabcoders/v2RbN/

    $(".box").scroll(function() { //.box is the class of the div
        $("span").css( "display", "inline" ).fadeOut( "slow" );
    });
    

    更新:http://jsfiddle.net/collabcoders/v2RbN/1/

    $("span").hide();
    
    $(".box").scroll(function() {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
            $("span").show();    
        } else {
            $("span").hide();
        }
    });
    

    【讨论】:

    • 我修改了我的问题先生,看看stackoverflow.com/questions/21212051/…
    • 成功了,非常感谢!你从我学校的校长和副校长那里救了我的命!
    • @johnnyarguelles : 真棒先生... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多