/** * input-focus.service: 解决安卓多输入框 呼出键盘被遮挡的问题 * */ import {Injectable} from '@angular/core'; import {Platform} from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class InputFocusServe { divDom = document.createElement('input-focus-fix-box'); pageDom; constructor(private platform: Platform) { } init() { if (this.platform.is('android')) { window.addEventListener('native.keyboardshow', (e: any) => { // alert('keyboardshow'); this.addDivDom(); this.domScrollIntoView(); }); window.addEventListener('native.keyboardhide', (e: any) => { // alert('keyboardhide'); this.removeDivDom(); }); } } domScrollIntoView() { this.addDivDom(); document.activeElement.scrollIntoView({block: 'center', behavior: 'smooth', inline: 'end'}); // console.log('document.activeElement0', document.activeElement); } addDivDom() { this.pageDom = this.findParentDom(document.activeElement, 'input-focus-fix'); this.pageDom.appendChild(this.divDom); } removeDivDom() { this.pageDom.removeChild(this.divDom); } findParentDom(domEl, className) { const dom = domEl.parentNode; // console.log('findParentDom', dom); if (dom.tagName === 'HTML') { console.warn('未匹配到dom'); return null; } const domClassName = dom.className || ''; if (dom.tagName === 'ION-CONTENT' || domClassName.indexOf(className) > -1) { return dom; } else { return this.findParentDom(dom, className); } } }