首先了解一下加入faskclick這個(gè)庫(kù)解決的問(wèn)題:
- click 300ms延遲:瀏覽器click會(huì)比touch延遲300ms觸發(fā)
- click穿透現(xiàn)象:當(dāng)兩個(gè)div同處一個(gè)position扰她,上層div綁定touch兽掰,下層div綁定click,當(dāng)上層div觸發(fā)touch消失后,可能會(huì)觸發(fā)下層div的click事件
bug描述:在頁(yè)面浮層中徒役,存在輸入框孽尽,在使用fastClick后,導(dǎo)致輸入框難以focus
解決方案:
import FastClick from 'fastclick'
import Helper from '@/util/helper'
FastClick.prototype.focus = function(targetElement) {
var length;
//兼容處理:在iOS7中忧勿,有一些元素(如date杉女、datetime、month等)在setSelectionRange會(huì)出現(xiàn)TypeError
//這是因?yàn)檫@些元素并沒(méi)有selectionStart和selectionEnd的整型數(shù)字屬性鸳吸,所以一旦引用就會(huì)報(bào)錯(cuò)熏挎,因此排除這些屬性才使用setSelectionRange方法
if (Helper.UA.isIos && targetElement.setSelectionRange && targetElement.type.indexOf('date') !== 0 && targetElement.type !== 'time' && targetElement.type !== 'month' && targetElement.type !== 'email') {
length = targetElement.value.length;
targetElement.setSelectionRange(length, length);
/*修復(fù)bug ios 11.3不彈出鍵盤(pán),這里加上聚焦代碼晌砾,讓其強(qiáng)制聚焦彈出鍵盤(pán)*/
targetElement.focus();
} else {
targetElement.focus();
}
};
export default FastClick