react 组件实现监听浏览器窗口变化,需要在window对象中添加监听事件,事件中参数1是监听的事件,参数2为事件调用的方法。
// 监听浏览器窗口变化
componentDidMount() {
// 在window对象中添加监听事件
window.addEventListener('resize', this.resizeWindow)
}
// 移除监听窗口变化的事件
componentWillUnmount() {
window.removeEventListener('resize', this.resizeWindow)
}
// 窗口变化执行的方法
resizeWindow = ()=>{
let screenWidth = document.body.offsetWidth
console.log(screenWidth)
if(screenWidth < 1200){
}else{
}
}
本文发布于:2025-04-05 09:18:00,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/1743815902584106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |