Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a resize event was triggered by soft keyboard in mobile browser?

There's a lot of discussion about the soft keyboard but I haven't found a good solution for my problem yet.

I have a resize function like:

$(window).resize(function() {     ///do stuff }); 

I want to do the 'stuff' in that function on all resize events except for when it is triggered by the soft keyboard. How can I determine if the soft keyboard has triggered the resize?

like image 259
Jose Calderon Avatar asked Feb 15 '13 19:02

Jose Calderon


People also ask

Which event is triggered if the browser window is resized?

The onresize event occurs when the browser window has been resized.

Which method occurs when window is resized?

The resize event occurs when the browser window changes size. The resize() method triggers the resize event, or attaches a function to run when a resize event occurs.

How do I capture a Windows resize event?

Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.


2 Answers

I recently ran into some problems that needed a check for this. I managed to solve it like so:

$(window).on('resize', function(){    // If the current active element is a text input, we can assume the soft keyboard is visible.    if($(document.activeElement).attr('type') === 'text') {       // Logic for while keyboard is shown    } else {       // Logic for while keyboard is hidden    } }); 

I only needed it for text inputs, but obviously this could be expanded for any kind of element which might trigger the soft keyboard/number picker etc.

like image 85
kirisu_kun Avatar answered Oct 01 '22 21:10

kirisu_kun


I've just fixed kirisu_kun's answer to use prop() instead of attr():

$(window).on('resize', function(){    // If the current active element is a text input, we can assume the soft keyboard is visible.    if($(document.activeElement).prop('type') === 'text') {       // Logic for while keyboard is shown    } else {       // Logic for while keyboard is hidden    } }); 
like image 23
CpnCrunch Avatar answered Oct 01 '22 22:10

CpnCrunch