如何在jquery中检测移动Web应用程序中的键盘显示/隐藏事件

Nit*_*ite 19 jquery

我正在开发基于Web的移动(HTML)应用程序.有没有办法检测键盘事件,如键盘可见和键盘隐藏,基于我可以控制其他屏幕布局.

我已经尝试了焦点,模糊,浏览器调整大小事件,但我的问题没有解决100%,所以我只寻找键盘事件,实际上我想在键盘可见时隐藏页脚,因为它(页脚)出现在键盘上,所以我试图在键盘可见时设置相对的页脚位置,并在键盘隐藏时将页脚位置固定.

我尝试过如下工作,但这不能解决我的问题.

$(document).ready(function () {

  $("input").focus(function() {
    $(".copyright_link").css("position","relative");    
  });      

  $("input").blur(function() {
    $(".copyright_link").css("position","fixed");   
  });      

});
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决页脚问题,或者让我知道jquery中是否有键盘事件.

sui*_*ish 17

如果键盘出现与否,您可以使用resize事件来获取

$(document).ready(function(){
  var _originalSize = $(window).width() + $(window).height()
  $(window).resize(function(){
    if($(window).width() + $(window).height() != _originalSize){
      console.log("keyboard show up");
      $(".copyright_link").css("position","relative");  
    }else{
      console.log("keyboard closed");
      $(".copyright_link").css("position","fixed");  
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

  • 它仅适用于我没有将设备纵向旋转到横向或反之亦然的情况.因为var _originalSize = $(window).width()+ $(window).height(); 在景观和肖像模式上有所不同,因此它按照上面的代码触发了不必要的页脚位置.因此,现在我想只寻找键盘事件..有没有? (2认同)

Alf*_*ani 7

if($(document.activeElement).attr('type') == "text"){
    console.log("Keyboard is visible");
}else{
    console.log("Keyboard is not visible");  
}
Run Code Online (Sandbox Code Playgroud)

  • 在Android上,你可以通过点击三角形隐藏键盘('隐藏键盘按钮').输入将保持活动状态. (12认同)

Dan*_*Dan 5

使用jQuery:

var lastWindowWidth = $(window).width(),
    lastWindowHeight = $(window).height();

$(window).resize(function() {

    var newWindowWidth = $(window).width(),
        newWindowHeight = $(window).height();

    if( newWindowHeight > lastWindowHeight && newWindowWidth == lastWindowWidth ) {

        // Keyboard closed
        // ...

    }

    lastWindowWidth = newWindowWidth;
    lastWindowHeight = newWindowHeight;

});
Run Code Online (Sandbox Code Playgroud)

请注意,当键盘动画关闭时,窗口调整大小事件(以及"键盘关闭"注释块)可能会被调用多次.编辑以满足您的需求.