1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| let judgeDeviceType = function () { let ua = window.navigator.userAgent.toLocaleLowerCase(); let isIOS = /iphone|ipad|ipod/.test(ua); let isAndroid = /android/.test(ua);
return { isIOS: isIOS, isAndroid: isAndroid } }()
function listenKeybord($input) { if (judgeDeviceType.isIOS) { $input.addEventListener('focus', function () { console.log('IOS 键盘弹起啦!'); }, false)
$input.addEventListener('blur', () => { console.log('IOS 键盘收起啦!'); }) }
if (judgeDeviceType.isAndroid) { let originHeight = document.documentElement.clientHeight || document.body.clientHeight;
window.addEventListener('resize', function () { let resizeHeight = document.documentElement.clientHeight || document.body.clientHeight; if (originHeight < resizeHeight) { console.log('Android 键盘收起啦!'); if (judgeDeviceType.isMiuiBrowser) { document.body.style.marginBottom = '0px'; } } else { console.log('Android 键盘弹起啦!'); if (judgeDeviceType.isMiuiBrowser) { document.body.style.marginBottom = '40px'; } }
originHeight = resizeHeight; }, false) } }
let $inputs = document.querySelectorAll('.input');
for (let i = 0; i < $inputs.length; i++) { listenKeybord($inputs[i]); }
|