近期做项目时候使用vant ui做的h5页面发现进去页面后点击输入框不太灵敏,点击多次输入框才能获取焦点,排查原因发现后是fastclick包的问题。
在vue项目中使用fastclick可以安装fastclick插件,安装使用方法如下
1.npm install --save fastclick
在main.js中使用
import FastClick from 'fastclick'
FastClick.prototype.focus = function (targetElement) {
let length;
//这是因为这些元素并没有selectionStart和selectionEnd的整型数字属性,所以一旦引用就会报错,因此排除这些属性才使用setSelectionRange方法
if (targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month') {
length = targetElement.value.length;
targetElement.focus();
targetElement.setSelectionRange(length, length);//修复bug ios 11.3不弹出键盘,这里加上聚焦代码,让其强制聚焦弹出键盘
} else {
targetElement.focus();
}
};
FastClick.attach(document.body);
这样就可以完美解决点击输入框不灵敏的问题啦啦啦。。。