【问题标题】:Javascript event (add style) on height scroll for Chrome, FF, IEChrome、FF、IE 的高度滚动上的 Javascript 事件(添加样式)
【发布时间】: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>

【问题讨论】:

标签: javascript html css


【解决方案1】:

IEdoesn't support scrollY.

您可以这样做:

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

【讨论】:

    【解决方案2】:

    我在 SO 上遇到了一个答案搜索

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

    积分归this SO link

    【讨论】:

    • 在这种情况下,它是重复的,应该这样标记。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2012-06-13
    • 1970-01-01
    • 2013-04-12
    • 2011-12-02
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多