使用 jQuery 获取垂直滚动的最大值



这是我的视图代码,我使用 jQuery 它可以工作,在我更改屏幕分辨率之前我没有任何问题,它停止工作。我知道问题在哪里,但我无法解决它,看看代码:

@model PNUBOOKIR.Models.ProductModels
@{
    Layout = null;           
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<script src="@Url.Content("~/Scripts/jquery.js")" type="text/javascript"></script>    
<script type="text/javascript">
    var endRun = false;
    var PageNO = 1;
    $(window).scroll(function () {
        if ((($("#container").outerHeight() - 796) == ($(this).scrollTop())) && (!endRun)) {
            endRun = true;
            Load();
        }
    });
    $(document).ready(function () {
        Load();
        return false;
    });
    function Load() {
        var BtnShowMore = document.getElementById("BtnShowMore");
        BtnShowMore.innerHTML = "Loading...";
        $.ajax({ type: "Post",
            url: "Product" + "/" + "GetHomeEventAjax",
            data: "{" + "PageNO" + ":" + PageNO + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function succ(data, states) {
                if (states == "success") {
                    if (data.Content != null) {
                        var EventListArea = document.getElementById("result");
                        $('#result > tbody:last').append(data.Content);
                        PageNO = PageNO + 50;
                        BtnShowMore.innerHTML = "More";
                        endRun = false;
                    }
                    else BtnShowMore.innerHTML = "Loading is complete";
                }
            },
            error: function err(result) { alert(result.responseText); }
        });
    }        
</script>
</head>
<body>
    <div id="container">
        <table cellpadding="4" cellspacing="2" border="1" style="width: 450px; direction: rtl;"
            id="result">
            <tbody>
                <tr>
                    <th>کد کالا</th>
                    <th>نام کتاب</th>
                    <th>ناشر</th>
                </tr>
            </tbody>
        </table>
        <a style="width: 200px" id="BtnShowMore">
            Load</a>
    </div>
</body>
</html>

在滚动事件脚本中,我需要找出垂直滚动的最大值是多少,所以我得到了容器外高,但并不是最大值,它在 796x1280 屏幕分辨率下比它大 1024px,在不同的屏幕分辨率设置中可以不同。我需要一些帮助,而不是"796"号码。

发布的答案不正确;scrollTop的最大值不是scrollHeight,而是scrollHeight - outerHeight


这将为您提供正确的值:

var elem = $("#container");
var maxScrollTop = elem[0].scrollHeight - elem.outerHeight();

尝试:

$("#container").attr({ scrollTop: $("#container").attr("scrollHeight") });

从 JQuery 1.9.1 开始,您将需要:

$("#container").scrollTop($("#container").prop("scrollHeight"));

不使用 jQuery 的最佳方法。

document.getElementById('container').scrollTop = document.getElementById('container').scrollHeight;

使用 jQuery 它对我不起作用,所以如果遇到上述问题,请尝试这个。

var contentHeight = 0;
var scrollBottom = $('#container').scrollTop() + $('#container').height();
$('#container').children().each(function() {
     contentHeight += $(this).height();
});
if (scrollBottom >= contentHeight) {
     // You have reached the bottom, do some stuff.
}

最新更新