javascript - Detect When Soft Keyboard is in Use abd Run a JS Function? -
i have site opens modal upon click contains form. noticed on ipad when soft keyboard open covers several fields of form, , near bottom of screen, cannot scroll reveal hidden fields.
in researching issue came across this answer includes code this answer. however, neither of these answers seem work when tested on ios 8.3.
here achieve:
- detect when keyboard opened.
- find height of keyboard.
- add padding bottom of footer accommodate height of keyboard.
- when keyboard dismissed or closed, padding removed.
there few things note:
- if using connected keyboard (including bluetooth keyboards) don't soft keyboard shouldn't appear
- jquery okay use.
- a solution must run via client side code.
- i prefer solution covers ios and android. preferably, device can use soft keyboard.
how achieve solution increase padding in footer, work on majority of devices, when using soft keyboard means of filling out form in modal?
i bumped problem similar not long ago , found small solution this, when mobile or tablet being used , keyboard activated triggers resize event of screen use trigger function.
var lastheight = $(window).height(); // store intial height. var lastwidth = $(window).width(); // store intial width. var keyboardison = false; $(window).resize(function () { if ($("input").is(":focus")) { keyboardison = ((lastwidth == $(window).width()) && (lastheight > $(window).height())); } if(keyboardison){ var keyboardheight = lastheight - $(window).height(); $("footer").css("padding", keyboardheight+"px"); } else{ $("footer").removeattr("style"); //or if want remove padding //$("footer").css("padding", 0); } }); //an alternative solution checking if height of screen //change on input/textarea focus. $('input, textarea').focus(function() { keyboardison = ((lastwidth == $(window).width()) && (lastheight > $(window).height())); if(keyboardison){ var keyboardheight = lastheight - $(window).height(); $("footer").css("padding", keyboardheight+"px"); } else{ $("footer").removeattr("style"); //or if want remove padding //$("footer").css("padding", 0); } });
Comments
Post a Comment