当前位置: 首页 > 知识库问答 >
问题:

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

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

  useEffect(() => {
    screen.orientation.addEventListener('change', handleChange);
    return () => {
      screen.orientation.removeEventListener("change", handleChange)
    }
  }, [handleChange])

共有1个答案

富波光
2023-06-09
const handleChange = ()=>{
    setTimeout(() => {
        console.log(document.documentElement.clientWidth)
    }, 0);
}

useEffect(() => {
    screen.orientation.addEventListener('change', handleChange);
    return () => {
      screen.orientation.removeEventListener("change", handleChange)
    }
}, [handleChange])
 类似资料: