gpt4 book ai didi

javascript - 如何检测Web中是否显示手机键盘?

转载 作者:行者123 更新时间:2023-11-28 03:08:30 25 4
gpt4 key购买 nike

我一直在使用下面的代码,但是当键盘可见时什么也没有发生。我已经检查过,window.screen.height 没有改变。

class TestPage extends React.Component {
constructor(props) {
super(props);

this.state = {
keyboardIsShown: false,
};
}

componentDidMount() {
this.initialScreenSize = window.screen.height;

this.keyboard = setInterval(() => {
if (this.initialScreenSize !== window.screen.height) {
!this.state.keyboardIsShown && this.setState({ keyboardIsShown: true });
} else {
this.state.keyboardIsShown && this.setState({ keyboardIsShown: false });
}
}, 800);
}

componentWillUnmount() {
clearInterval(this.keyboard);
}

...
}

最佳答案

通过使用 window.innerHeight 解决了 Android 设备的问题。

class TestPage extends React.Component {
constructor(props) {
super(props);

this.state = {
keyboardIsShown: false,
};
}

componentDidMount() {
this.initialScreenSize = window.innerHeight;

this.keyboard = setInterval(() => {
if (this.initialScreenSize !== window.innerHeight) {
!this.state.keyboardIsShown && this.setState({ keyboardIsShown: true });
} else {
this.state.keyboardIsShown && this.setState({ keyboardIsShown: false });
}
}, 800);
}

componentWillUnmount() {
clearInterval(this.keyboard);
}

...
}

关于javascript - 如何检测Web中是否显示手机键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60371961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com