javascript 为什么 window.addEventListener('scroll', this.someScrollHandler, false) 在 IE 10 上不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31439443/
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-28 13:47:25  来源:igfitidea点击:

Why doesn't window.addEventListener('scroll', this.someScrollHandler, false) work on IE 10?

javascriptinternet-explorerjavascript-eventsreactjs

提问by jcjcjcjc

I'm currently building a React app with a scroll handler for loading more data in an infinite scroll component. I'm using window.addEventListener('scroll', this.someScrollHandler, false);(with throttling), which works on every browser except for IE — no event is handled.

我目前正在构建一个带有滚动处理程序的 React 应用程序,用于在无限滚动组件中加载更多数据。我正在使用window.addEventListener('scroll', this.someScrollHandler, false);(带节流),它适用于除 IE 之外的所有浏览器——不处理任何事件。

In fact, testing in the IE console, the below code, then scrolling, results in no logging:

事实上,在 IE 控制台中测试,下面的代码,然后滚动,导致没有日志记录:

window.addEventListener('scroll', function() { console.log('testing') }, false);

What's going on with scroll events and IE?

滚动事件和 IE 发生了什么?

回答by Daantje

My problem was that I had the body height 100%, that disabled the scroll event.

我的问题是我的身体高度为 100%,这禁用了滚动事件。

body {
   height: 100%; // <-- will disable window.addEventListener('scroll')
}

回答by jcjcjcjc

After a lot of debugging, the problem was in the css. The app is responsive, so we had a base overflow-x: hiddenstyle, then switching to overflow-x: initialafter a breakpoint. Apparently IE doesn't like initial, so it was still picking up on the overflow hidden, thus preventing scroll events from firing. Switching to overflow-x: visiblefixed the problem.

经过大量调试,问题出在css上。该应用程序是响应式的,所以我们有一个基本overflow-x: hidden样式,然后overflow-x: initial在断点之后切换到。显然 IE 不喜欢初始,所以它仍然在隐藏溢出,从而防止滚动事件触发。切换到overflow-x: visible修复了问题。

回答by Julien

This is a cross-browser scroll event listener (it disables scrolling, but it should work if you replace preventDefault by your handler):

这是一个跨浏览器滚动事件侦听器(它禁用滚动,但如果您将 preventDefault 替换为您的处理程序,它应该可以工作):

function disableScroll() {
  if (window.addEventListener) // older FF
    window.addEventListener('DOMMouseScroll', preventDefault, false);
  window.onwheel = preventDefault; // modern standard
  window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
  window.ontouchmove  = preventDefault; // mobile
  document.onkeydown  = preventDefaultForScrollKeys;
}

You can see there are a lot of different handlers...

你可以看到有很多不同的处理程序......