Home > Web Front-end > JS Tutorial > body text

VUE long press event requirement example sharing

小云云
Release: 2018-01-09 13:41:36
Original
2249 people have browsed it

This article mainly introduces the needs of several long press events in detail for everyone. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

In development, there is often a need for long press events. Here I will briefly introduce the requirements for several long press events.

Requirement 1: Long press the numbers to add up or down

HTML:


<p class="mui-numbox" data-numbox-step=&#39;10&#39; data-numbox-min=&#39;0&#39; data-numbox-max=&#39;100&#39;>
   <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>
</p>
Copy after login

JS :


var vm = new Vue({     
  el: "#vue-container",
  data:{
    Loop:null
  },
  methods:{//长按添加数量
    Loop_Add:function(ID){
      //设置数量
      clearInterval(vm.Loop);//再次清空定时器,防止重复注册定时器
      $target=$(event.target).parent().find(&#39;input&#39;);
      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(&#39;input&#39;);
      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);
    }
  }
})
Copy after login

This demo was tested on the mobile terminal, so the touch event is used. The method is very simple. Register an Interval timer when you touchstart, and clear the timer when you touchend. In this way, you can achieve the effect of continuous accumulation or decrement of long press.

Requirement two: Long press delay event trigger

This type of requirement is also relatively simple, similar to requirement one. Take requirement 1 as an example. Just add a setTimeout timer to touchstart to delay event execution, and touchend to clear the timer.

Related recommendations:

Vue implements the method example of vertically centering long images on top and short images vertically centering

vue uses axios Detailed explanation of cross-domain request data examples

How to use Vue high-order components

The above is the detailed content of VUE long press event requirement example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!