1、例子使用场景 屏幕旋转后 重新设置悬浮球的位置
this.getDirection()
window.addEventListener('resize', this.getDirection) //监听屏幕旋转触发方法
getDirection = () => {
const w = window.document.documentElement.clientWidth //获取当前屏幕宽
const h = window.document.documentElement.clientHeight //获取当前屏幕高
this.setState({ oLeft: w - 107, oTop: h / 2 })
}
//卸载 componentWillUnmount在组件从 DOM 中移除之前立刻被调用
componentWillUnmount() {
window.removeEventListener('resize', this.getDirection)
}