VUE长按事件需求详解
2017-10-18 11:31
861 查看
在开发中常常会有长按事件的需求,这里我简单的介绍几种长按事件的需求。
需求一:长按数字累加或者累减
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清除计时器即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- UIView的Touch事件UIControlEvents详解
- 《JavaScript高级程序设计 第三版》学习笔记 (十一)事件详解
- Android事件分发机制详解
- Android触摸屏事件派发机制详解与源码分析一(View篇)
- DOM事件阶段以及事件捕获与事件冒泡先后执行顺序(图文详解)
- Android View的事件分发详解
- 详解JavaScript中的自定义事件编写
- jQuery事件绑定用法详解
- iOS事件响应链详解(The Responder Chain)
- jquery移除、绑定、触发元素事件使用示例详解
- Android事件分发详解(五)——Touch事件传递验证
- [html]事件处理addEventListener详解
- C# 委托与事件详解
- Spring 4.2框架中注释驱动的事件监听器详解
- android事件传递机制以及onInterceptTouchEvent()和onTouchEvent()详解二之小秘与领导的故事
- 移动web(三)touch事件详解
- HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)
- android事件分发机制详解
- js中鼠标滚轮事件详解
- JavaScript Array对象详解和项目需求