一、事件基礎
PC:click、mouseover、mouseout、mouseenter、mouseleave、mousemove、mousedown、mouseup、mousewheel、keydown、keyup、load、scroll、blur、focus、change...
移動端:click(單擊)、load、scroll、blur、focus、change、input(代替keyup、keydown)...TOUCH事件模型(處理單手指操作)、GESTURE事件模型(處理多手指操作)
TOUCH:touchstart、touchmove、touchend、touchcancel
GESTURE:gesturestart、gesturechange、gestureend
1、click:在移動端click屬于單擊事件,不是點擊事件,在移動端的項目中我們經常會區分單擊做什么和雙擊做什么,所以移動端的瀏覽器在識別click的時候,只有確定是單擊后才會把它執行:
在移動端使用click會存在300ms的延遲:瀏覽器在第一次點擊結束后,還需要等到300ms看是否觸發了第二次點擊,如果觸發了第二次點擊就不屬于click了,沒有觸發第二次點擊才屬于click
下面代碼是移動端模擬click時間的代碼
function on(curEle,type,fn){ curEle.addEventListener(type,fn,false); } var oBox = document.querySelector('.box'); //移動端采用click存在300ms延遲 // oBox.addEventListener('click',function(){ // this.style.webkitTransform = 'rotate(360deg)' // },false) //使用TOUCH事件模型實現點擊操作(單擊&&雙擊) on(oBox,'touchstart',function(ev){ //ev:TouchEvent事件 屬性 type、target、preventDefault(returnValue)、stopPropagation、changedTouches、touches //changedTouches和touches都是手指信息的集合(touchList),touches獲取到值的必要條件只有手指還在屏幕上才可以獲取,所以在touchend事件中如果想獲取手指離開的瞬間坐標只能使用changedTouches獲取 var point = ev.touches[0]; this['strX'] = point.clientX; this['strY'] = point.clientY; this['isMove'] = false; }) on(oBox,'touchmove',function(ev){ var point = ev.touches[0]; var newX = point.clientX, newY = point.clientY; //判斷是否發生滑動,我們需要判斷偏移的值是否在30PX以內 if(Math.abs(newX-this['strX'])>30 || Math.abs(newY-this['strY'])>30){ this['isMove'] = true; } }) on(oBox,'touchend',function(ev){ if(this['isMove'] === false){ //沒有發生移動 點擊 this.style.webkitTransitionDuration = '1s'; this.style.webkitTransform = 'rotate(360deg)'; var delayTimer = window.setTimeout(function(){ this.style.webkitTransitionDuration = '0s'; this.style.webkitTransform = 'rotate(0deg)'; }.bind(this),1000); }else{ //滑動 this.style.background = 'red'; } })同時也可以使用fastclick.js來解決移動端click事件的300ms延遲 (github地址https://github.com/zhouxiaotian/fastclick)
2、點擊、單擊、雙擊、長按、滑動、左滑、右滑、上滑、下滑
單擊和雙擊(300MS)
點擊和長按(750MS)
點擊和滑動(X/Y軸偏移的距離是否在30PX以內,超過30PX就是滑動)
新聞熱點
疑難解答
圖片精選