Javascript未在页面末尾触发

Javascript not triggered at the end of the page

本文关键字:Javascript      更新时间:2023-09-26

我试图在下面的页面末尾触发警报,但只要我滚动,它就会一直触发。我尝试了几种不同的方法,但每次警报都会在页面底部之前触发。代码中缺少的只是一个触发大约 20 个结果的数据库调用、标头和显示结果的 php 包含。显示的结果显示大约 4 个屏幕的数据。

<body>
    <div class="maincontainer">
        <div class="profilecontainer">
            <row>
                <column cols="4" class="left">
                    This will have text
                </column>
                <column cols="8" class="right">
                    <ul class="post-column">
                        <?php 
                        foreach ($data as $lineitem):
                        $type = $lineitem['type'];
                        if($type == "article"){
                           require('php/articleitem.php');
                        }
                        endforeach; 
                        ?>
                    </ul>
                </column>
            </row>
        </div>
    </div>
</body>
</html>
<script>
    $(window).scroll(function () {
        if ($(document).height() <= $(window).scrollTop() + $(window).height()) {
            alert("End Of The Page");
        }
    });
</script>

window.innerHeight + document.body.scrollTop大于或等于document.body.offsetHeight时,你就在底部

我需要设置以下 css 值。

.body {
   position: absolute;
    min-height: 100%;
    min-width: 100%;
}

当您的 HTML 顶部没有文档类型时,$(window).height()的结果将不符合预期:

<!DOCTYPE html>

添加它,您将不再很快收到警报。