VUE长按事件需求详解


Posted in Javascript onOctober 18, 2017

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

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

VUE长按事件需求详解

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清除计时器即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Jquery AutoComplete自动完成 的使用方法实例
Mar 19 Javascript
jQuery EasyUI 中文API Button使用实例
Apr 14 Javascript
jQuery+CSS实现菜单滑动伸展收缩(仿淘宝)
Mar 22 Javascript
jquery实现对联广告的方法
Feb 05 Javascript
基于javascript的异步编程实例详解
Apr 10 Javascript
在vue中给列表中的奇数行添加class的实现方法
Sep 05 Javascript
基于Angular中ng-controller父子级嵌套的相关属性详解
Oct 08 Javascript
webpack开发环境和生产环境的深入理解
Nov 08 Javascript
vue使用自定义指令实现拖拽
Jan 29 Javascript
Vuex modules模式下mapState/mapMutations的操作实例
Oct 17 Javascript
layui实现显示数据表格、搜索和修改功能示例
Jun 03 Javascript
vue elementui tree 任意级别拖拽功能代码
Aug 31 Javascript
vue router仿天猫底部导航栏功能
Oct 18 #Javascript
Node做中转服务器转发接口
Oct 18 #Javascript
Vue组件之Tooltip的示例代码
Oct 18 #Javascript
JS正则表达式完美实现身份证校验功能
Oct 18 #Javascript
详解vue项目首页加载速度优化
Oct 18 #Javascript
JS简单实现滑动加载数据的方法示例
Oct 18 #Javascript
详解cordova打包成webapp的方法
Oct 18 #Javascript
You might like
Wordpress php 分页代码
2009/10/21 PHP
深入php多态的实现详解
2013/06/09 PHP
php实现微信公众号无限群发
2015/10/11 PHP
PHP7如何开启Opcode打造强悍性能详解
2018/05/11 PHP
Laravel框架Eloquent ORM删除数据操作示例
2019/12/03 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
jquery插件之定时查询待处理任务数量
2014/05/01 Javascript
jquery滚动加载数据的方法
2015/03/09 Javascript
javascript单例模式的简单实现方法
2015/07/25 Javascript
js实现select下拉框菜单
2015/12/08 Javascript
js判断鼠标位置是否在某个div中的方法
2016/02/26 Javascript
AngularJS中isolate scope的用法分析
2016/11/22 Javascript
jQuery使用正则表达式替换dom元素标签用法示例
2017/01/16 Javascript
原生js二级联动效果
2017/06/20 Javascript
vue-resource 拦截器(interceptor)的使用详解
2017/07/04 Javascript
NodeJs实现定时任务的示例代码
2017/12/05 NodeJs
JS+CSS实现滚动数字时钟效果
2017/12/25 Javascript
vue实现一个6个输入框的验证码输入组件功能的实例代码
2020/06/29 Javascript
深入理解Python中的内置常量
2017/05/20 Python
python3 判断列表是一个空列表的方法
2018/05/04 Python
Python实现随机创建电话号码的方法示例
2018/12/07 Python
Python设计模式之解释器模式原理与用法实例分析
2019/01/10 Python
python实现大文件分割与合并
2019/07/22 Python
pymysql 开启调试模式的实现
2019/09/24 Python
python 经典数字滤波实例
2019/12/16 Python
在django admin详情表单显示中添加自定义控件的实现
2020/03/11 Python
python matplotlib 绘图 和 dpi对应关系详解
2020/03/14 Python
python实现opencv+scoket网络实时图传
2020/03/20 Python
Python基于QQ邮箱实现SSL发送
2020/04/26 Python
美国和加拿大房车出售在线分类广告:RVT.com
2018/04/23 全球购物
在线购买澳大利亚设计师手拿包和奢华晚装手袋:Olga Berg
2019/03/20 全球购物
行政办公员自我评价分享
2013/12/14 职场文书
开会迟到检讨书
2014/02/03 职场文书
2014年社区民政工作总结
2014/12/02 职场文书
汤姆索亚历险记读书笔记
2015/06/29 职场文书
nginx配置之并发频次限制
2022/04/18 Servers