简体   繁体   English

IE 在滚动时通过普通 JavaScript 向 div 添加类的问题

[英]Issue on IE adding class to a div via plain JavaScript on scrolling

I'm using the following javascript to perform adding a "sticky" class to "features-menu" div after scrolling down on browser certain amount.在浏览器向下滚动一定数量后,我正在使用以下 javascript 向“功能菜单”div 添加“粘性”类。

Through search a while ago, I came across this code from another user I believe from stackoverflow, but cannot find that post to reference it.不久前通过搜索,我从另一个我相信来自 stackoverflow 的用户那里发现了这段代码,但找不到该帖子来引用它。 But I was looking for an example that does not use jQuery.但我正在寻找一个不使用 jQuery 的示例。

The following works great in Firefox, Chrome, and MS Edge browser but on IE11, the class doesn't seem to get added to the div.以下在 Firefox、Chrome 和 MS Edge 浏览器中效果很好,但在 IE11 上,该类似乎没有添加到 div 中。

Can anyone kindly suggest a fix for IE without using jQuery?任何人都可以在不使用 jQuery 的情况下建议修复 IE 吗? Thank you so much!非常感谢!

<script type = "text/javascript" >

myID = document.getElementById("features-menu");

var myScrollFunc = function() {

  var y = window.scrollY;

  if (y >= 400) {
    myID.className = "sticky"
  } else {
    myID.className = ""
  }
};

window.addEventListener("scroll", myScrollFunc);

</script>

If you look in the web console, you'll see an error telling you that addEventListener isn't a function on older IE.如果您查看 Web 控制台,您会看到一条错误消息,告诉您addEventListener不是旧版 IE 上的函数。 And unfortunately, IE11 will hobble itself if you've looked at it funny, going into (in)Compatibility Mode, and not have addEventListener (or any of several other modern features).不幸的是,如果您觉得 IE11 很有趣,进入(处于)兼容模式,并且没有addEventListener (或其他几个现代功能中的任何一个),那么 IE11 就会步履蹒跚。 For instance, if your page is on an internal network, by default IE will use Compatibility Mode and act like IE7 — and fail because IE7 didn't have addEventListener .例如,如果您的页面位于内部网络上,则默认情况下 IE 将使用兼容模式并像 IE7 一样运行 — 并且会失败,因为 IE7 没有addEventListener

You can test for that and use attachEvent instead:您可以对此进行测试并使用attachEvent代替:

if (window.addEventListener) {
    window.addEventListener("scroll", myScrollFunc);
} else {
    window.attachEvent("onscroll", myScrollFunc);
}

(Note the "on".) (注意“开”。)

This kind of browser inconsistency, combined with the DOM being quite verbose and awkward, is why we have libraries like jQuery.这种浏览器的不一致,再加上 DOM 非常冗长和笨拙,这就是我们拥有 jQuery 之类的库的原因。 :-) (Not saying you should use jQuery [or any other lib], just saying this kind of thing is a major reason they exist.) :-)(不是说你应该使用 jQuery [或任何其他库],只是说这种事情是它们存在的一个主要原因。)

Note that attachEvent and addEventListener are not exactly the same, but both hook up events and for what you've shown, they'll behave the same.请注意, attachEventaddEventListener并不完全相同,但两者都连接事件,并且对于您显示的内容,它们的行为是相同的。 For a more thorough handling of the differences, see the hookEvent function in this answer .要更彻底地处理差异,请参阅此答案中hookEvent函数。

The problem is with window.scrollY which is undefined in IE.问题出在 IE 中未定义的window.scrollY This is an alternative approach :这是另一种方法:

Demo演示

var y = window.scrollY || window.pageYOffset;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM