【问题标题】:Checking if scroll is at bottom in jquery in Google Chrome检查滚动是否在谷歌浏览器的 jquery 底部
【发布时间】:2020-07-13 06:45:30
【问题描述】:

我有 django 项目,但无法让 jquery 脚本在 google chrome 中运行。

检查滚动是否在底部的简单代码:

 <script language="JavaScript" type="text/JavaScript">
    window.onload = function () {

       $('#scrolling').on('scroll', chk_scroll);
    };

    function chk_scroll(e) {

        var elem = $(e.currentTarget);
        if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
           alert("bottom")
        }
    }
</script>

它适用于 Opera、Explorer、Firefox、Chrome(作为单个 html 文件,不是项目的一部分)、jsfiddle。

P.s jquery 正确加载,其他脚本正常工作。

【问题讨论】:

  • 你试过用$( document ).ready()代替window.onload吗?
  • 是的,结果和 setTImeout 一样
  • 我不一定认为这是问题所在,但是您的语言和类型属性中的值是驼峰式的,而它们应该小写。更好的是,对于普通的脚本标签,这些标签可以省略。
  • 这有什么帮助吗? stackoverflow.com/a/28287574/7867822
  • 感谢您的建议,学习新事物并发现问题。 Anurag,你的链接真的很有帮助!它在带有 cntrl+scroll 的缩放浏览器中。我的 Chrome 默认缩放到 105%,这 5% 是问题。我只需要计算调整大小

标签: javascript jquery django google-chrome


【解决方案1】:

尝试以下方法:

$( window ).on( 'scroll', checkScroll );


function checkScroll()
{
    const documentScrollTop     = $( document ).scrollTop();
    const documentOuterHeight   = $( document ).outerHeight();
    const windowOuterHeight     = $( window ).outerHeight();

    if ( documentScrollTop >= documentOuterHeight - windowOuterHeight )
    {
        //bottom
    }
}

你不需要使用任何元素,除了窗口来检查你是否已经触底

【讨论】:

    猜你喜欢
    • 2015-07-30
    • 2012-03-15
    • 2012-01-21
    • 2011-07-13
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    相关资源
    最近更新 更多