VUE长按事件

PS:在开发中常常会有长按事件的需求,这里我简单的介绍几种长按事件的需求

 

需求一:长按数字累加或者累减

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:

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的时候再把定时器清除掉,这样就能实现长按持续累加或者累减的效果。

需求二:长按延时事件触发

这类需求也比较简单,和需求一类似。这里拿需求一举例,只需在touchstart添加setTimeout计时器延时事件执行,touchend清除计时器即可。

 

posted on 2017-10-18 11:24  一曲笙箫  阅读(770)  评论(0编辑  收藏  举报