如何检查滚动条的状态已经处于顶部或在结束了吗?(How to check the scroll ba

2019-07-17 22:20发布

当用户设定显示在滚动条“溢出:汽车;” 并且用户可以滚动的东西从顶部到底部。 问题是,如何能在使用Javascript / jQuery的检查时滚动条或者是在底部或顶部? 以便

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

那么,有没有任何功能可按/办法检查这种状态? 谢谢

更新:不使用working- jQuery用户界面对话框

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;
        }); 

Answer 1:

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!");
        }
    });
});


Answer 2:

校验

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


文章来源: How to check the scroll bar status is already at top or at end?