关注公众号: 微信搜索 前端工具人 ; 收货更多的干货
1. ios中,输入框获得焦点时,页面输入框被遮盖,定位的元素位置错乱:
当页
input存在于吸顶或者吸底元素中时,用户点击输入框,输入法弹出后,fiexd失效,页面中定位好的元素随屏幕滚动。
解决方案如下:
方案一: Web API 接口 :scrollIntoView 的应用,将input输入框显示在可视区域。
// 输入框获得焦点时,元素移动到可视区域
inputOnFocus(e) {
setTimeout(function(){
e.target.scrollIntoView(true);
// true:元素的顶端将和其所在滚动区的可视区域的顶端对齐; false:底端对齐。
},200); // 延时 == 键盘弹起需要时间
}
方案二:在输入框获得焦点时,将页面滑动到最底部,避免fixed导致的页面乱飞,并且保证input在最底部。
var timer;
// 输入框获得焦点时,将元素设置为position:static,设置timer
inputOnFocus(e) {
e.target.style.className = 'input input-static';
timer = setInterval(
function() {
document.body.scrollTop = document.body.scrollHeight
}, 100)
};
// 输入框失去焦点时,将元素设置为 position:fixed,清除timer
inputOnbulr(e) {
e.target.parentNode.className = 'input input-fixed';
clearInterval(timer)
};
当获得焦点弹出虚拟键盘后,input输入框会一直紧贴键盘顶部。如果,你的页面弹出输入法后不需要滑动查看其他内容,那么你对这种方案应该很中意。
方案三:将页面进行拆分: 页面(main) = 内容(sectionA) + 输入框(sectionB)+ 其他(sectionOther)
原理 : main.height = window.screen.height ;
sectionA 绝对定位,进行内部滚动 overflow-y:scroll ;
sectionB 可保证在页面最底部。
.main { position: relative; height: 100%; }
.sectionA { box-sizing: border-box; padding-bottom: 60px; height: 100%; overflow-y: scroll; -webkit-overflow-scrolling: touch //为了使滚动流畅,sectionA 添加属性 }
.sectionB { position: absolute; height: 60px; overflow: hidden; left: 0; right: 0; bottom: 0; }
纯css3打造,可以滚动,可以固定位置,基本满足大部分布局需要。
2. 阻止旋转屏幕时自动调整字体大小
移动端开发时,屏幕有竖屏和横屏模式,当屏幕进行旋转时,字体大小则有可能会发生变化,从而影响页面布局的整体样式,为避免此类情况发生,只需设置如下样式即可
* {
-webkit-text-size-adjust: none;
}
3. iOS下取消input在输入的时候英文首字母的默认大
<input type="text" autocapitalize="none">
4. 禁止 iOS 识别长串数字为电话
<meta name="format-detection" content="telephone=no" />
5. 禁止 iOS 弹出各种操作窗口
-webkit-touch-callout: none;
6. 禁止ios和android用户选中文字
-webkit-user-select: none;
7. 消除transition闪屏问题
/*设置内嵌的元素在 3D 空间如何呈现:保留 3D*/
-webkit-transform-style: preserve-3d;
/*(设置进行转换的元素的背面在面对用户时是否可见:隐藏)*/
-webkit-backface-visibility: hidden;
8. CSS动画页面闪白,动画卡顿
-webkit-transform: translate3d(0, 0, 0);
-moz-transform: translate3d(0, 0, 0);
-ms-transform: translate3d(0, 0, 0);
transform: translate3d(0, 0, 0);
9. input的placeholder会出现文本位置偏上的情况
input 的placeholder会出现文本位置偏上的情况:PC端设置line-height等于height能够对齐,而移动端仍然是偏上,解决方案时是设置css
line-height:normal;
10. IOS 中单行输入框输入内容长被遮盖,不能显示全部,且不能左右滑动。
这个是IOS的一个bug,可以考虑用 textarea 替换 input,设置一行的高,进行上下滚动查看。(其他方案可以参看下面 第 6 点)
11. 获得焦点时,光标消失或错位:
-webkit-user-select:none导致input框在iOS中无法输入,光标不出现,设置如下
user-select: text;
-webkit-user-select: text;
- 利用scrollIntoView 使当前元素出现到指定位置,避免光标错位,设置如下:
e.target.scrollIntoView(true);
e.target.scrollIntoViewIfNeeded();
12. 进入页面如何自动获取焦点,弹出软键盘?
- 添加 autofocus 属性 支持自动获得焦点
- 触发 focus() 事件
13.随文字输入,输入框宽度自适应。
onkeyPress(e) {
const testLength = e.target.value.length;
e.target.style.width = `${testLength*8+10}px`
}
testLength * 8 英文字符,testLength * 16中文字符, +10为后边光标预留位置。 这种方案显然不适用于对精确度有很高要求的需求。
14. 介绍一个属性:contenteditable,模拟输入时动态获取宽高
- (1)div设置contentditable=true 可以将此元素变成可输入状态。
<div class="inputContent" contenteditable="true" ></div>
- (2)想要变成input输入框,利用css模拟输入框的样式
.inputContent{
color: #444;
border: #999 solid 1px;
border-radius: 3px;
padding: 5px 10px;
box-sizing: border-box;
min-width: 50px;
max-width: 300px;
background: #ffffff;
}
这里配合min-width,max-width 效果更真实。
-(3)点击div可以弹出软键盘,但是无法输入内容,需要设置属性,如下
.inputContent{
user-select:text;
-webkit-user-select:text;
}
这样就完成一个可以根据获取输入内容来动态来调节宽高。
还可以利用js模拟placeholder等,这里就不展开了
15.其他问题及解决
- 输入框获得焦点可弹出软键盘,却没有光标闪烁,也无法正常输入。
-webkit-user-select:none 导致的,可以这样解决
*:not(input,textarea) {
-webkit-touch-callout: none;
-webkit-user-select: none;
}
- input 自定义样式
// 使用伪类
input::-webkit-input-placeholder,
input::-moz-placeholder,
input::-ms-input-placeholder {
...style
text-align: center;
}
16. ios下 输入框获取焦点后,失去焦点后页面不回弹,顶部上移了,底部留有一截灰色区域,需要手动随意触摸一个地方。才会回弹。
解决方法:监听软键盘弹起、关闭事件,在进行对应的操作
mounted () {// 软键盘关闭事件 可在全局的地方 vue的话比如App.vue 文件里添加以下代码
document.body.addEventListener('focusout', () => {
// 回到原点 若觉得一瞬间回到底部不够炫酷,那自己可以自定义缓慢回弹效果, 可用css 、贝塞尔曲线、js的 requestAnimationFrame 等等 方法 实现体验性更好的回弹效果
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
})
}