javascript - jquery检测div滚动

标签 javascript jquery content-management-system scroll

我想检测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,但它什么也没输出,而不是两个。

最佳答案

不确定,但您可以在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();
    }
});

关于javascript - jquery检测div滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21211787/

相关文章:

javascript - React.js 什么是 "ctx"对象,它来自哪里?

javascript - 同时加载多个 $.ajax 调用

javascript - 我如何在页面加载本身的依赖下拉列表中加载默认国家、州和城市?

php - 如何从 php 数组创建 jQuery if 语句?

javascript - 通过input type = file上传文件如何打开windows对话框窗口

content-management-system - 从谷歌搜索中删除旧站点索引

php - 如何在 Silverstripe 中循环模板

javascript - 函数前的加号是什么?

javascript - 使用 jQuery 追加为兄弟元素而不是子元素

java - 是否完全使用 Adob​​e CQ5 (AEM) 编程进行开发?