javascript $(document).scroll 仅在 IE8 中不触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7534277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:26:25  来源:igfitidea点击:

$(document).scroll is not firing in IE8 only

javascriptjqueryinternet-explorer-8scroll

提问by MTsrb

I have a site running some javascript. In IE8 only, the $(document).scroll is not firing when you scroll with or without the mousewheel. Code snippet below:

我有一个运行一些 javascript 的站点。仅在 IE8 中,无论是否使用鼠标滚轮滚动,$(document).scroll 都不会触发。下面的代码片段:

$(document).scroll(function () {
        //do something on scroll
      });

Is there a specific reason this function won't fire in IE8? I have searched online with no success.

此功能在 IE8 中不会触发是否有特定原因?我在网上搜索过,没有成功。

Thanks for all advice and tips in advance!!!!!

感谢您提前提供所有建议和提示!!!!!

回答by Naftali aka Neal

Try using window:

尝试使用window

  $(window).scroll(function () {
    //do something on scroll
  });

回答by Chris Disley

For a lot of areas, IE ties the event to window rather than document as other browsers will. $(window).scroll(function(e) {}); is what you're after here. Should generally also work in most other browsers too, but if not, use a check on the navigator to find IE and use window or document based on that Boolean.

对于许多领域,IE 将事件与窗口关联,而不是像其他浏览器那样将事件关联到文档。$(window).scroll(function(e) {}); 是你在这里追求的。通常也应该在大多数其他浏览器中工作,但如果不是,请检查导航器以查找 IE 并使用基于该布尔值的窗口或文档。