js學習總結----移動端事件基礎及常用的事件庫

來源:互聯網
上載者:User

標籤:--   doc   http   math   hlist   wip   out   https   動手   

一、事件基礎

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就是滑動)

  左右滑動和上下滑動(X軸位移的距離 > Y軸位移的距離 = 左右滑 相反就是上下滑)

  左滑和右滑(位移的距離 >0 = 右滑  相反就是左滑)

二、常用的事件庫

  FastClick.js :解決CLICK事件300MS的延遲

  TOUCH.js:百度雲移動手勢庫  GitHub地址  https://github.com/Clouda-team/touch.code.baidu.com

  執行個體如下:  

var oBox = document.querySelector(‘.box‘);        //單擊        touch.on(oBox,‘tap‘,function(ev){            this.style.webkitTransitionDuration = ‘1s‘;            this.style.webkitTransform = ‘rotate(360deg)‘;            var delayTimer = window.setTimeout(function(){                this.style.webkitTransitionDuration = ‘0s‘;                this.style.webkitTransform = ‘rotate(0deg)‘;                window.clearTimeout(delayTimer)            }.bind(this),1000)        })        //雙擊        touch.on(oBox,‘doubletap‘,function(ev){            this.style.webkitTransitionDuration = ‘1s‘;            this.style.webkitTransform = ‘rotate(-360deg)‘;            var delayTimer = window.setTimeout(function(){                this.style.webkitTransitionDuration = ‘0s‘;                this.style.webkitTransform = ‘rotate(0deg)‘;                window.clearTimeout(delayTimer)            }.bind(this),1000)        })        //長按        touch.on(oBox,‘hold‘,function(ev){            this.style.backgroundColor = ‘red‘;        })

  HAMMER.js

  Zepto.js:被譽為移動端的小型JQ,JQ由於是在PC端使用的,所以代碼中包含了大量對於ie低版本瀏覽器的相容處理,而ZEPTO只應用在移動端開發,所以在JQ的基礎上沒有對低版本的ie進行支援

  JQ中提供了很多的選取器類型及DOM操作方法,但是ZEPTO只是實現了部分常用的選取器和方法。例如:JQ中的動畫方法有animate、hide、show、fadeIn、fadeOut、fadeToggle、slideDown、slideUp、slideToggle...但是在ZEPTO中只有animate

  ZEPTO的原始碼大小比JQ小很多

  ZEPTO專門為移動端開發而誕生,所以相對於JQ來說更適合移動端:

  ZEPTO的animate動畫方法支援了CSS3動畫的操作

  ZEPTO專門的準備了移動端常用 的事件操作:tap、singleTap、doubleTap、longTap、swipe、swipeUp、swipeDown、swipeLeft、swipeRight

  執行個體代碼如下:  

$(‘.box‘).singleTap(function(ev){            $(this).animate({                rotate:‘360deg‘            },1000,‘linear‘,function(){                this.style.webkitTransform = ‘rotate(0)‘            })        })        $(‘.box‘).on(‘touchstart‘,function(){            $(this).css(‘background‘,‘red‘)        })        $.ajax({            url:‘‘,            type:‘get‘,            dataType:‘json‘,            cache:false,            success:function(){                            }        })

js學習總結----移動端事件基礎及常用的事件庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.