【发布时间】:2020-02-12 08:06:04
【问题描述】:
我正在尝试更改容器的高度,仅在移动横向模式下。我在开发人员工具中玩转移动设备的方向,但它只适用于第一次渲染。我是反应钩子的新手,所以不确定我是否正确实施。
我的想法是,我在横向测试一次,如果它在移动设备上,高度应该小于 450 像素(这是我为 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);
};
}, []);
【问题讨论】:
标签: javascript reactjs react-hooks