screen.orientation.addEventListener('change')拿不到最新的document的宽度?

const handleChange = ()=>{
    // h5横竖屏切换的时候,这个宽度有时候拿到的还是之前的值
    console.log(document.documentElement.clientWidth)
}

  useEffect(() => {
    screen.orientation.addEventListener('change', handleChange);
    return () => {
      screen.orientation.removeEventListener("change", handleChange)
    }
  }, [handleChange])
阅读 1.4k
1 个回答
const handleChange = ()=>{
    setTimeout(() => {
        console.log(document.documentElement.clientWidth)
    }, 0);
}

useEffect(() => {
    screen.orientation.addEventListener('change', handleChange);
    return () => {
      screen.orientation.removeEventListener("change", handleChange)
    }
}, [handleChange])
推荐问题
logo
Microsoft
子站问答
访问
宣传栏