我试图在特定高度滚动时触发事件。 特别是你添加了一个样式,我的代码在 Chrome 中工作,但在 IE 中不工作。 有人可以帮忙吗?
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>
最佳答案
您可以这样做:
'scrollY' in window ? window.scrollY : document.documentElement.scrollTop
关于Chrome、FF、IE 高度滚动上的 Javascript 事件(添加样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943281/