如何检查滚动条状态是在顶部还是结束?

时间:2013-01-07 09:34:22

标签: javascript jquery html scroll scrollbar

当用户设置“overflow:auto;”时会显示滚动条。用户可以从上到下滚动。问题是,当滚动条位于底部或顶部时,javascript / jquery如何检查?那么

if (is_scrollbar_top || is_scrollbar_end)
//do some thing..

那么有没有任何功能/方法来检查这种状态?感谢

更新:不工作 - 使用jquery ui对话框

HTML:

<div class = "dialog" id="dialog" title="Past Issues"></div>

的javascript:

$('#pastIssues').click(function (event) {
            var issueString = 'product=Headline&year=2012&month=12';
            $('.issues,#issuesBox').remove();
            var dWidth = $(window).width() * 0.9; 
            var dHeight = $(window).height() * 0.9;

            $( "#dialog" ).dialog({
                    height: dHeight,
                    width: dWidth,
                    modal: true,
                    draggable: false, 
                    resizable: false,
            });

            get_past_issues(issueString,2012,12,event.type);
            return false;
        }); 

2 个答案:

答案 0 :(得分:11)

HTML:

<div id="mydiv" style="overflow: auto; height: 500px"></div>

SCRIPT:

$(document).ready(function()
{
    $("#mydiv").scroll(function()
    {
        var div = $(this);
        if (div[0].scrollHeight - div.scrollTop() == div.height())
        {
            alert("Reached the bottom!");
        }
        else if(div.scrollTop() == 0)
        {
            alert("Reached the top!");
        }
    });
});

答案 1 :(得分:6)

检查

if($(window).scrollTop() == 0 || $(window).scrollTop() == $(document).height()- $(window).height()) {
   // do something
}