Get viewport height when soft keyboard is on

后端 未结 7 1806
梦如初夏
梦如初夏 2021-01-31 08:45

I\'ve been trying to make this work for days now, with no luck, so I thought I\'d share info and maybe someone comes up with a solution.

I want to get the exact viewport

相关标签:
7条回答
  • 2021-01-31 09:51

    Piece attempts to return approximate window.innerWidth, window.innerHeight values at ios6 ua; jquery utilized for selectors and .on(). Not certain about detecting ios device keyboard events portion; see resources links

    Try

    css

    html {
        height : 100vh;
        width : 100vw;
    }
    

    js

    $(function() {    
        if (/ipad|iphone/gi.test(window.navigator.userAgent)) {
        var events = "abort blur focus input scroll submit touchstart touchmove";
        $("form, input").on(events, function(e) {
          return (function(window, elem, w, h) {
                   var vh = window.getComputedStyle(elem,null).getPropertyValue(h);
                   var vw = window.getComputedStyle(elem,null).getPropertyValue(w);
                   var vwh = { 
                               "documentWidth": vw, 
                               "documentHeight": vh, 
                               "windowInnerWidth": window.innerWidth, 
                               "windowInnerHeight": window.innerHeight
                             };
                   console.log(vwh);
                   var _vwh = document.getElementById("vwh");
                   _vwh.innerHTML = JSON.stringify(vwh)
                   return vwh 
                  }(window, document.documentElement, "width", "height"));
        }).focus();
        };
    })
    

    jsfiddle http://jsfiddle.net/guest271314/Pv3N2/

    resources

    https://developer.mozilla.org/en-US/docs/Web/API/Window.innerHeight

    http://www.w3.org/TR/2013/CR-css3-values-20130730/#vh-unit

    how to handle key events in iphone

    https://coderwall.com/p/xuawww Responding to Keyboard Events on iOS

    http://looksok.wordpress.com/2013/02/02/ios-tutorial-hide-keyboard-after-return-done-key-press-in-uitextfield/ iOS tutorial: hide keyboard after return / done key press in UITextField

    https://developer.apple.com/library/safari/documentation/InternetWeb/Conceptual/SafariVisualEffectsProgGuide/InteractiveControl/InteractiveControl.html#//apple_ref/doc/uid/TP40008032-CH16

    http://www.quirksmode.org/mobile/viewports2.html

    0 讨论(0)
提交回复
热议问题