admin管理员组文章数量:1026232
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{
}
}
版权声明:本文标题:react 组件监听浏览器窗口变化 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1743815901a2034617.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论