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
There are a few things u need to concentrate about
You can use these combinations when the browser gets resized
function getWidth(){
return $( window ).width();
}
function getHeight(){
return $( window ).height();
}
function isFocus(){
return $(document.activeElement).prop('tagName')=='INPUT' || $(document.activeElement).prop('tagName')=='TEXTAREA';
}
var focused = false;
var windowWidth=getWidth(),windowHeight=getHeight();
//then on resize...
$(window).resize(function() {
var tWidth=getWidth(),tHeight=getHeight(),tfocused=isFocus();
//if the saved window width is still equal to the current window width do nothing
if (windowWidth == tWidth && ((tHeight < windowHeight && !focused && tfocused) || (tHeight > windowHeight && focused && !tfocused))){
windowWidth=tWidth;
windowHeight=tHeight;
focused=tfocused;
return;
}
else{
windowWidth=tWidth;
windowHeight=tHeight;
focused=tfocused;
//Your Code Here
}
});
This question relies on there being a reliable way to detect when the onscreen keyboard appears (or disappears) on mobile devices. Unfortunately, there is no reliable way to detect this. Similar questions have come up several times on SO, with various hacks, tricks and workarounds suggested (see this answer for links to several relevant answer threads).
Also note that the resize event is not always triggered when the onscreen keyboard appears (see this answer).
My general suggestion would be to detect presence of touchscreen + detection of whether the active element is of a type that triggers an onscreen keyboard (something similar to this answer). However, this approach would still fail for hybrid windows devices (such as Surface Pro), where sometimes the onscreen keyboard may be present on browser resize, and sometimes the hardware keyboard may be in use on browser resize.
I've been looking for a solution to a similar issue. My resize event was triggering when the url input came in and out of view. This is something I've been working on... Could have a possible solution?
So basically you just check if the width of the screen alone has changed or not, and only fire your functions on the resize if it is different:
eg:
var saveWindowWidth = true;
var savedWindowWidth;
//set the initial window width
if (saveWindowWidth = true){
savedWindowWidth = windowWidth;
saveWindowWidth = false;
}
//then on resize...
$(window).resize(function() {
//if the screen has resized then the window width variable will update
windowWidth = window.innerWidth;
//if the saved window width is still equal to the current window width do nothing
if (savedWindowWidth == windowWidth){
return;
}
//if saved window width not equal to the current window width do something
if(savedWindowWidth != windowWidth) {
// do something
savedWindowWidth = windowWidth;
}
});
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
}
}
The problem is that, if the active element is focused, you can trigger the resize event just by closing the keyboard without altering the focus.. so, the keyboard will hidden but the code will enter into the condition of focus.
Working fine in current version of chrome webview. I just implemented window resize functionality in Angular by using below code.
@HostListener('window:resize', ['$event'])
onResize(event) {
// Do you handling here.
}
Note: Same can be achieved by using
window.addEventListener('resize', () => {
// Do handling here
});