mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
This requires us to set fullscreen="false" in our cordova apps. Uses the resize event to determine when the keyboard has been shown, then broadcasts an event from the activeElement: 'scrollChildIntoView', which is caught by the nearest parent scrollView. The scrollView will then see if that element is within the new device's height (since the keyboard resizes the screen), and if not scroll it into view. Additionally, when the keyboard resizes the screen we add a `.hide-footer` class to the body, which will hide tabbars and footer bars while the keyboard is opened. For now, this is android only. Closes #314.
53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
(function(ionic) {
|
|
|
|
ionic.Platform.ready(function() {
|
|
if (ionic.Platform.is('android')) {
|
|
androidKeyboardFix();
|
|
}
|
|
});
|
|
|
|
function androidKeyboardFix() {
|
|
var rememberedDeviceWidth = window.innerWidth;
|
|
var rememberedDeviceHeight = window.innerHeight;
|
|
var keyboardHeight;
|
|
|
|
window.addEventListener('resize', resize);
|
|
|
|
function resize() {
|
|
|
|
//If the width of the window changes, we have an orientation change
|
|
if (rememberedDeviceWidth !== window.innerWidth) {
|
|
rememberedDeviceWidth = window.innerWidth;
|
|
rememberedDeviceHeight = window.innerHeight;
|
|
console.info('orientation change. deviceWidth =', rememberedDeviceWidth,
|
|
', deviceHeight =', rememberedDeviceHeight);
|
|
|
|
//If the height changes, and it's less than before, we have a keyboard open
|
|
} else if (rememberedDeviceHeight !== window.innerHeight &&
|
|
window.innerHeight < rememberedDeviceHeight) {
|
|
document.body.classList.add('hide-footer');
|
|
//Wait for next frame so document.activeElement is set
|
|
window.rAF(handleKeyboardChange);
|
|
} else {
|
|
//Otherwise we have a keyboard close or a *really* weird resize
|
|
document.body.classList.remove('hide-footer');
|
|
}
|
|
|
|
function handleKeyboardChange() {
|
|
//keyboard opens
|
|
keyboardHeight = rememberedDeviceHeight - window.innerHeight;
|
|
var activeEl = document.activeElement;
|
|
if (activeEl) {
|
|
//This event is caught by the nearest parent scrollView
|
|
//of the activeElement
|
|
ionic.trigger('scrollChildIntoView', {
|
|
target: activeEl
|
|
}, true);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
})(window.ionic);
|