JS 长按事件

匿名 (未验证) 提交于 2019-12-02 23:47:01
$.fn.longPress = function(fn) {     var timeout = undefined;     var $this = this;     for(var i = 0;i<$this.length;i++){         $this[i].addEventListener('touchstart', function(event) {             timeout = setTimeout(fn, 800);  //长按时间超过800ms,则执行传入的方法             }, false);         $this[i].addEventListener('touchend', function(event) {             clearTimeout(timeout);  //长按时间少于800ms,不会执行传入的方法             }, false);     } }

首先要添加这段代码,然后调用:

$('.object').longPress(function(){     //do something... });

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!