首页 > 解决方案 > 类不会在滚动时切换

问题描述

有人可以帮助我吗?每当我尝试滚动时,类 none 都不会切换..

    window.addEventListener('scroll', function() {
    var header = document.getElementsById("here");
    header.classList.toggle("none", window.scrollY > 0);
});
.none {
    display: none;
}
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png" id="here>

标签: javascripthtml

解决方案


你简单地没有关闭 img 的 id 并写elementsbyid而不是elementbyid.

window.addEventListener('scroll', function() {
    var header = document.getElementById("here");
    header.classList.toggle("none", window.scrollY > 0);
});
.none {
    display: none;
}
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png" id="here">


推荐阅读