【笔记】jq实现长按监听效果

1、通过jq添加一个封装好的长按监听函数

$.fn.longPress = function (fn,trsTime) {//长按监听
	var $this = this;
	for (var i = 0; i < $this.length; i++) {
		(function (target) {
			var timeout;
			target.addEventListener('touchstart', function (event) {
				timeout = setTimeout(function () {
					//fn.apply(target);
					fn(event);
				}, trsTime ? trsTime : 500);
			}, false);
			target.addEventListener('touchend', function (event) {
				clearTimeout(timeout);
			}, false);
		})($this[i]);
	}
}

2、给需要实现长按效果的标签加监听事件

$(".qrCode").longPress(function (e) {//长按执行
	console.log("长按2秒输出这句话");
},2000)
发布了22 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_42618289/article/details/103918633