js mobile terminal solves the problem that the keyboard pops up after input focus, and the height is squeezed by the edge of the page

The page style is a percentage layout, and the width and height are all 100%. The tested mobile device is a Huawei tablet. When the keyboard is popped up by clicking the input box, the page style height will be compressed and squeezed. Solve the code:

    //解决弹出键盘页面高度变化bug

    var viewWidth = window.innerWidth; //获取可视区域宽度
    var viewHeight = window.innerHeight; //获取可视区域高度
    if (viewWidth > viewHeight) {
        horizontalScreen = true; // 横屏 判断横竖屏,需要的时候用
    } else {
        horizontalScreen = false; // 竖屏
    }
    $("input").focus(function () { // 监听获取焦点事件
        $("body").css("height", viewHeight); //键盘弹起的时候把body高度设置可视高度
    }).blur(function () {
        $("body").css("height", "100%");
    });

The browser is initialized in the vertical screen. After the screen is rotated to the horizontal screen, it will be deformed when the input box is clicked. So I listened to the browser window size change and re-assigned the visual height.

window.addEventListener('resize', function () {
    // 变化后需要做的事
    console.log("bianhuale==============");
    viewHeight = window.innerHeight; //获取可视区域高度
    viewWidth = window.innerWidth; //获取可视区域高度
})

 

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/113941022