9

当用户设置“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;
        }); 
4

2 回答 2

12

HTML:

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

脚本:

$(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!");
        }
    });
});
于 2013-01-07T09:44:55.023 回答
6

查看

if($(window).scrollTop() == 0 || $(window).scrollTop() == $(document).height()- $(window).height()) {
   // do something
}
于 2013-01-07T10:19:15.507 回答