How to detect keyboard show/ hide event in jquery for mobile web application

后端 未结 5 1943
故里飘歌
故里飘歌 2020-12-30 01:00

I am working on web base mobile (HTML) application. Is there any way to detect keyboard event like when keyboard is visible and keyboard hide, base on that I can control o

5条回答
  •  Happy的楠姐
    2020-12-30 01:21

    Using 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;
    
    });
    

    Note that the window resize event (and thus the "Keyboard closed" comment block) may get called several times as the keyboard animates closed. Edit to suit your needs.

提交回复
热议问题