最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

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

业界 admin 12浏览 0评论

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

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{
       
   }
}
发布评论

评论列表 (0)

  1. 暂无评论