0

I am trying to trigger an event on scroll at certain height. Specifically yo add a style, and my code works in Chrome, but not in IE. Can anyone assist?

myID = document.getElementById("subnav");
var myScrollFunc2 = function() {
  var y = window.scrollY;
  if (y >= 150) {
    myID.className = "subnav stick";
  } else {
    myID.className = "subnav unstick";
  }
};
window.addEventListener("scroll", myScrollFunc2);
.subnav {
  width: 100%;
  z-index: 100;
  background-color: #ffffff;
}

.stick {
  top: -62px;
  position: fixed !important;
}

.unstick {
  position: relative !important;
}
<div id="subnav">123</div>
Zenoo
  • 12,670
  • 4
  • 45
  • 69

2 Answers2

0

IE doesn't support scrollY.

You can do this instead:

'scrollY' in window ? window.scrollY : document.documentElement.scrollTop
We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
0

I've came accross an answer searching on SO

var scroll = 
    window.scrollY // Modern Way (Chrome, Firefox)
 || window.pageYOffset // (Modern IE, including IE11)
 || document.documentElement.scrollTop // (Old IE, 6,7,8)

Credits goes to this SO link

jonatjano
  • 3,576
  • 1
  • 15
  • 20
Jaay
  • 2,103
  • 1
  • 16
  • 34