How to check the scroll bar status is already at top or at end?

≯℡__Kan透↙ 提交于 2019-12-30 08:17:14

问题


The scroll bar is shown when user set "overflow:auto;" and the user can scroll things from top to bottom. The problem is , how can the javascript/jquery check when the scroll bar is either at the bottom or at the top? So that

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

So are there any funciton/ way to check such status? Thanks

Updated: Not working- using jquery ui dialog

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

回答1:


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



回答2:


check

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


来源:https://stackoverflow.com/questions/14193193/how-to-check-the-scroll-bar-status-is-already-at-top-or-at-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!