我试图仅在移动横向模式下更改容器的高度。我正在开发人员工具中尝试交换移动设备的方向,但它仅适用于第一次渲染。我是反应钩子的新手,所以不确定我是否正确实现了它。
我的想法是,我正在测试一旦处于横向状态,如果在移动设备上,高度应小于 450px(这是我为 if 语句所做的检查)
有人能给我指出正确的方向吗?
谢谢!
const bikeImageHeight = () => {
const windowViewportHeight = window.innerHeight;
const isLandscape = window.orientation === 90 || window.orientation === -90;
let bikeImgHeight = 0;
if (windowViewportHeight <= 450 && isLandscape) {
bikeImgHeight = windowViewportHeight - 50;
}
return bikeImgHeight;
};
useEffect(() => {
bikeImageHeight();
window.addEventListener("resize", bikeImageHeight);
return () => {
window.removeEventListener("resize", bikeImageHeight);
};
}, []);Run Code Online (Sandbox Code Playgroud)