UNPKG

keyboard-height

Version:

calaculate the height of soft keyboard in mobile phone and emit on changes

91 lines (72 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true, }); exports.default = _default; exports.getAndroidHeight = getAndroidHeight; exports.getAndroidInnerHight = getAndroidInnerHight; /** * android 下用 innerHeight 对于新开 tab 会有问题,会把上面的导航条也算进去, toolbar 大约在 150 px 以内 * 即便在 DCL 下获取,也是有问题的,我们尝试在 1s 内轮训获取真实的 innerHeight * 同时要避免 autoFocus 导致页面刚打开就谈键盘,引起计算 bug,同理,部分键盘上有刷新按钮,导致页面刚打开的 innerHeight 就不对 */ var initialHeight = innerHeight; var realInnerHeight = innerHeight; // 键盘未弹起时的高度 function reset() { initialHeight = innerHeight; realInnerHeight = innerHeight; } var lastScreenHeight = screen.height; // 用于探测是否旋转屏幕,只要是容器变了都算 var maxCheckTime = 10; var toolbarMaxSize = 150; var checkCnt = 0; var callback = function callback() {}; var intval = setInterval(function () { checkCnt++; if (checkCnt > maxCheckTime) clearInterval(intval); else { var currentInnerHeight = innerHeight; if (initialHeight - currentInnerHeight > toolbarMaxSize) return; // 可能键盘已经弹起 if (realInnerHeight === currentInnerHeight) return; // 无变化 realInnerHeight = currentInnerHeight; // 通知 callback(); } }, 100); function isOrientationChanged() { var changed = false; if (!isSame(lastScreenHeight, screen.height)) { changed = true; lastScreenHeight = screen.height; } return changed; } function isSame(a, b) { return Math.abs(a - b) < 0.01; } var lastHeight = -1; function _default(cbArr) { []; callback = function callback() { if (isOrientationChanged()) { reset(); if (lastHeight > 0) realInnerHeight = innerHeight + lastHeight; } else { var delta = getAndroidHeight(); if (isSame(delta, lastHeight)) return; lastHeight = delta; cbArr.forEach(function (cb) { cb.call(null, lastHeight); }); } }; window.addEventListener("resize", callback); } function getAndroidHeight() { var currentInnerHeight = innerHeight; if (currentInnerHeight > realInnerHeight && checkCnt >= maxCheckTime) realInnerHeight = currentInnerHeight; return realInnerHeight - currentInnerHeight; } function getAndroidInnerHight() { return realInnerHeight; }