我有一个 Ionic 4 应用程序,其中有一个包含输入的表单。
当用户单击输入时,它会打开键盘,但会隐藏内容,而不滚动。
有没有办法解决?
这是我的代码:
<form #f="ngForm" (ngSubmit)="sendMail()">
<ion-item>
<ion-label position="floating">name
</ion-label>
<ion-input [(ngModel)]="senderName">
</ion-input>
</ion-item>
<ion-item>
<ion-label position="floating">mail
</ion-label>
<ion-input [(ngModel)]="senderMail">
</ion-input>
</ion-item>
<ion-item class="borderedTextArea">
<ion-textarea [(ngModel)]="mailText" style="height:150px;"></ion-textarea>
</ion-item>
<ion-button type="submit" style="float:left">send</ion-button>
</form>
我目前正在使用 Ionic4 和 Cordova 9 以及所有最新的软件包,但我在框架中找不到任何适合我的设置。最后我做了这个完全绕过框架的解决方法。它有一些动画,看起来还不错,所以我一直使用它,直到框架正确解决这个问题。
从“jquery”导入*作为$;
全局.scss
ion-app {
/*animation of native keyboard show*/
transition: margin 300ms;
}
应用程序组件.ts
declare var $: any;
ngAfterViewInit() {
// This element never changes.
let ionapp = document.getElementsByTagName("ion-app")[0];
window.addEventListener('keyboardDidShow', async (event) => {
// Move ion-app up, to give room for keyboard
let kbHeight: number = event["keyboardHeight"];
let viewportHeight: number = $(window).height();
let inputFieldOffsetFromBottomViewPort: number = viewportHeight - $(':focus')[0].getBoundingClientRect().bottom;
let inputScrollPixels = kbHeight - inputFieldOffsetFromBottomViewPort;
// Set margin to give space for native keyboard.
ionapp.style["margin-bottom"] = kbHeight.toString() + "px";
// But this diminishes ion-content and may hide the input field...
if (inputScrollPixels > 0) {
// ...so, get the ionScroll element from ion-content and scroll correspondingly
// The current ion-content element is always the last. If there are tabs or other hidden ion-content elements, they will go above.
let ionScroll = await $("ion-content").last()[0].getScrollElement();
setTimeout(() => {
$(ionScroll).animate({
scrollTop: ionScroll.scrollTop + inputScrollPixels
}, 300);
}, 300); // Matches scroll animation from css.
}
});
window.addEventListener('keyboardDidHide', () => {
// Move ion-app down again
// Scroll not necessary.
ionapp.style["margin-bottom"] = "0px";
});
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)