keyboard-height
Version:
calaculate the height of soft keyboard in mobile phone and emit on changes
32 lines (30 loc) • 739 B
JavaScript
// innerHeight 在 ios 下可能变化,比如导航条有时候收起、展开
const vp = window["visualViewport"];
function isSame(a, b) {
return Math.abs(a - b) < 0.01;
}
let lastHeight = -1;
export default function(cbArr) {
if (!vp || typeof vp !== "object") return;
function emit() {
var delta = getIosHeight();
if (isSame(delta, lastHeight)) return;
lastHeight = delta;
cbArr.forEach(cb => {
cb.call(null, lastHeight);
});
}
vp.onscroll = function() {
emit();
};
vp.onresize = function() {
emit();
};
}
export function getIosHeight() {
if (!vp) return 0;
return document.documentElement.clientHeight - vp.height;
}
export function getIosInnerHeight() {
return innerHeight;
}