Mobile keyboard changes html viewport size

安稳与你 提交于 2019-12-10 14:52:24

问题


A known problem if you are using percentage (or viewport unit) width and height for <body> is that when mobile keyboard invoke due to input the size of any percentage/viewport element will change .

I've searched a lot about this problem and nothing could really help.

I found one of the answer is to adjust the layout according to the new viewport : mobile viewport resize due to keyboard

but this is not really practical.

Does anybody know how to manipulate mobile keyboard on web ?

After some test's I found a hack that i'll put in the answers, if there are better ways, please tell :)


回答1:


Use JavaScript/jQuery to set the height and width of <body> in px.

Using this code:

$(function() {
    var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
    var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
    $("html, body").css({"width":w,"height":h});
});

In this case <body> will be set in px according to the viewport size and will stay constant with any changes to the viewport.

If the keyboard covers the input you can easily change the position of the input to fixed and top to 0.



来源:https://stackoverflow.com/questions/39585950/mobile-keyboard-changes-html-viewport-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!