VUE長按事件

PS:在開發中經常會有長按事件的需求,這裏我簡單的介紹幾種長按事件的需求vue

 

需求一:長按數字累加或者累減oop

HTML:測試

<div class="mui-numbox" data-numbox-step='10' data-numbox-min='0' data-numbox-max='100'>
     <button class="mui-btn mui-numbox-btn-minus" type="button"@touchstart="Loop_Sub(item.CartID)" @touchend="clearLoop()">-</button>
     <input class="mui-numbox-input" type="number" :value="item.Cart_Nums"/>
     <button class="mui-btn mui-numbox-btn-plus" type="button" @touchstart="Loop_Add(item.CartID)" @touchend="clearLoop()">+</button>
</div>

JS:ui

var vm = new Vue({         
  el: "#vue-container",
   data:{
    
Loop:null
  
},
  methods:{
//長按添加數量
    
Loop_Add:function(ID){
      
//設置數量
      clearInterval(vm.Loop);//再次清空定時器,防止重複註冊定時器
      
$target=$(event.target).parent().find('input');
      vm.Loop
=setInterval(function(){
      $num
=$target.val();
      $target.val(parseInt($num)
+1);
      
},100);
    },
    //長按減小數量   Loop_Sub:function(ID){
      
//設置數量
      
clearInterval(vm.Loop);//再次清空定時器,防止重複註冊定時器
      
$target=$(event.target).parent().find('input');
      vm.Loop
=setInterval(function(){
        $num
=$target.val();
        
if($num>0){
          $target.val(parseInt($num)
-1);
        }
else{
          clearInterval(vm.Loop);
        }   
//改變點擊數
      
},100);
    },
    clearLoop:function(){
      clearInterval(vm.Loop);
    }
  }
})

 這個Demo是在移動端測試的,所以使用的是touch事件。方法很簡單,touchstart的時候去註冊個Interval定時器,touchend的時候再把定時器清除掉,這樣就能實現長按持續累加或者累減的效果。spa

需求二:長按延時事件觸發code

這類需求也比較簡單,和需求一相似。這裏拿需求一舉例,只需在touchstart添加setTimeout計時器延時事件執行,touchend清除計時器便可。blog

相關文章
相關標籤/搜索