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

问题描述 投票:0回答:4

这是我使用 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>

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

javascript jquery jquery-events
4个回答
42
投票

贴出的答案不正确;

scrollTop
的最大值不是
scrollHeight
,它是
scrollHeight - outerHeight
.


这会给你正确的价值:

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

13
投票

尝试:

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

从 JQuery 1.9.1 开始,您将需要:

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

3
投票

不使用 jQuery 的最佳方式。

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

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


0
投票
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.
}
© www.soinside.com 2019 - 2024. All rights reserved.