我一直在使用下面的代码,但是当键盘可见时什么也没有发生。
我已经检查过,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/