结构以下 css
咱们须要作的就是当聚焦评论框的时候,ios
须要让键盘顶起评论框。在ios
系统中,当键盘弹起的时候,会挤压页面,评论框会天然在顶部,可是有个问题就是,下面的评论框会不贴底,露出下面的东西,因此在ios12
以前的解决办法就是在评论框触发focus
的时候让页面滚动到底部,代码以下:java
const body = document.dcumentElement.scrollTop ? document.documentELement : document.body;
const {scrollHeight, scrollTop} = body;
const innerHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
body.scrollTop = scrollHeight - innerHeight;
复制代码
若是输入框失去焦点,就让页面滚动到先前的位置。 代码以下:ios
body.scrollTop = scrollTop; // 滚动到先前的位置
复制代码
这种方案在ios12
上会出现两个问题:布局
而后我本身分析了一下这个问题,出现各类状况的缘由是由于弹出键盘时,页面可以滚动,因而就出现了各类问题,那干脆让页面没法滚动。ios11
及以前使用了下面的布局:ui
.parent {
position: fixed;
left: 0;
right: 0;
bottom: 0;
top: 0;
}
复制代码
而且禁止了touchmove
事件,这样可以让页面没法滚动,可是ios12
并无什么卵用。仍是可以滚动,那我们就让内容就一屏,多的被截掉。下面是输入框focus
的代码:spa
const {scrollHeight,scrollTop} = body;
const innerHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
body.style.height = `${innerHeight}px`;
body.style.overflow = 'hidden';
复制代码
而后就是输入框触发blur
事件时的代码:code
body.style.height = `${scrollHeight}px`;
body.style.overflow = 'auto';
body.style.scrollTop = scrollTop;
复制代码
在这里须要从新设置body
的高度,高度为以前获取的scrollHeight
,由于咱们须要从新滚动到先前的位置,建议不要设置height
为auto
,由于在一些场景下咱们可能须要监听滚动事件,会出现其余的问题(稳战稳打才能打胜仗)。而后从新设置body
的overflow
,让页面可以滚动,最后滚动到先前的位置。cdn