gpt4 book ai didi

Chrome、FF、IE 高度滚动上的 Javascript 事件(添加样式)

转载 作者:行者123 更新时间:2023-11-28 01:14:20 24 4
gpt4 key购买 nike

我试图在特定高度滚动时触发事件。特别是你添加了一个样式,我的代码在 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>

最佳答案

IE doesn't support scrollY .

您可以这样做:

'scrollY' in window ? window.scrollY : document.documentElement.scrollTop

关于Chrome、FF、IE 高度滚动上的 Javascript 事件(添加样式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51943281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com