javascript - 如何基于innerWidth JavaScript native添加或删除函数
问题描述
目前我正在尝试在屏幕宽度大于或小于时添加/删除功能,但下面的代码不起作用,有什么建议吗?
window.addEventListener('resize', () => {
if(window.innerHeight > 768){
window.addEventListener('scroll', scrolling);
}else{
window.removeEventListener('scroll', scrolling);
sectionFixedNavLeftSide.firstElementChild.classList.remove('fixed-sidebar-home');
sectionFixedNavLeftSide.firstElementChild.classList.remove('fixed-bottom-sidebar')
}
});
回调滚动:
const scrolling = () => {
sectionFixedNavLeftSide.getBoundingClientRect().top <= (screen.width /
50 * 2.7) ?
sectionFixedNavLeftSide.firstElementChild.classList.add('fixed-
sidebar-home') :
sectionFixedNavLeftSide.firstElementChild.classList.remove('fixed-
sidebar-home');
sectionFixedNavRightSide.getBoundingClientRect().bottom - screen.width
/ 2.85 <= 0 ?
sectionFixedNavLeftSide.firstElementChild.classList.add('fixed-bottom-
sidebar') :
sectionFixedNavLeftSide.firstElementChild.classList.remove('fixed-
bottom-sidebar');
}
解决方案
推荐阅读
- ios - 防止子视图被导航栏隐藏
- cookies - Chrome / Vivaldi 上的 Google Colaboratory ServiceWorker 错误
- php - 从 WooCommerce 消息中删除链接
- reactjs - 在 React 中设置 Medium Feed
- javascript - html5隐藏所有div并在按钮单击时显示一个
- kubernetes - Kubernetes maxPods int32 还包括其默认的 pod
- macos - 使用 Open Firmware 设置硬件默认值
- java - 两个Fragment之间的接口通信
- nlp - 用于 CRF 分类器的 StanfordNLP 训练迭代
- r - 如何使用 quantmode 向多个烛台图表添加简单的移动平均线