react 组件监听浏览器窗口变化

react 组件监听浏览器窗口变化 react 组件实现监听浏览器窗口变化,需要在window对象中添加监听事件,事件中参数1是监听的事件,参数2为事件调用的方法。

react 组件监听浏览器窗口变化

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{
       
   }
}

发布者:admin,转转请注明出处:http://www.yc00.com/web/1743815900a4511887.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信