UNPKG

keyboard-height

Version:

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

49 lines (38 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; exports.getIosHeight = getIosHeight; exports.getIosInnerHeight = getIosInnerHeight; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } // innerHeight 在 ios 下可能变化,比如导航条有时候收起、展开 var vp = window["visualViewport"]; function isSame(a, b) { return Math.abs(a - b) < 0.01; } var lastHeight = -1; function _default(cbArr) { if (!vp || _typeof(vp) !== "object") return; function emit() { var delta = getIosHeight(); if (isSame(delta, lastHeight)) return; lastHeight = delta; cbArr.forEach(function (cb) { cb.call(null, lastHeight); }); } vp.onscroll = function () { emit(); }; vp.onresize = function () { emit(); }; } function getIosHeight() { if (!vp) return 0; return document.documentElement.clientHeight - vp.height; } function getIosInnerHeight() { return innerHeight; }