javascript - 类不会在滚动时切换
问题描述
有人可以帮助我吗?每当我尝试滚动时,类 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>
解决方案
你简单地没有关闭 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">
推荐阅读
- if-statement - 有没有办法根据一列中的条件计算一行中的项目数?
- c# - JSON 嵌套数组。C# DataContractJsonSerializer 返回 null
- visual-studio-code - 问:我可以将 VSCode 配置为从我的主目录获取 .clang 格式的文件吗?
- perl - 删除 sed/awk/perl 中的最后一个分隔符
- python - 普通 Pandas Dataframe Pivot
- c# - 有没有办法简化这个三元表达式?
- google-apps-script - 根据输入的条件显示过滤的游戏列表的脚本?(谷歌电子表格
- angular - 如何在 Ionic4 的 Angular 6 中的变量中设置 storageservice 值“store_id”
- laravel - 如何将 base64 图像转换为 UploadedFile Laravel
- google-bigquery - BigQueryHook 作业失败并出现错误:7916072