【发布时间】:2018-08-21 15:23:57
【问题描述】:
我试图在特定高度滚动时触发事件。 特别是添加样式,我的代码在 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>
【问题讨论】:
-
y = document.documentElement.scrollTop + document.body.scrollTop;
标签: javascript html css