vue2.0 v-tap簡潔(漏)版 (只解決300ms問題)


Vue.directive('tap',{
    bind:function(el,binding){
        var startTx, startTy, endTx, endTy, startTime, endTime;

        el.addEventListener("touchstart",function(e){
            var touch=e.touches[0];
            startTx = touch.clientX;
            startTy = touch.clientY;
            startTime = +new Date()
        },false );

        el.addEventListener("touchend",function(e){
            endTime = +new Date()
            if (endTime - startTime > 300) {
                // 若點擊事件過長,不執行回調
                return
            }
            var touch = e.changedTouches[0];
            endTx = touch.clientX;
            endTy = touch.clientY;
            if( Math.abs(startTx - endTx) < 6 && Math.abs(startTy - endTy) < 6){
                // 若點擊期間手機移動距離過長,不執行回調
                var method = binding.value.method;
                var params = binding.value.params;
                method(params);
            }
        },false);
    }
})
使用方法:在創建Vue實例前引入,栗子:
<div id="test"  v-tap="{method:test,params:some}">


var vm= new Vue({
    el:"#test",
   data:{
       some:"ok!" 
   }, 
   method:{
      test:function(ee){
          console.log(ee)
      } 
    } 
})

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM