jQuery-Extensions-touchJS

jQuery-Extensions-touchJS是一个非常简单的jQuery touch扩展,用于适配移动端的常用touch操作(点击tab、长按longPress、滑动left right up down)

Tính đến 08-11-2022. Xem phiên bản mới nhất.

Script này sẽ không được không được cài đặt trực tiếp. Nó là một thư viện cho các script khác để bao gồm các chỉ thị meta // @require https://update.greasyfork.org/scripts/454450/1114729/jQuery-Extensions-touchJS.js

// ==UserScript==
// @name         jQuery-Extensions-touchJS
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  jQuery-Extensions-touchJS是一个非常简单的jQuery touch扩展,用于适配移动端的常用touch操作(点击tab、长按longPress、滑动left right up down)
// @author       tutu辣么可爱(greasyfork)/IcedWatermelonJuice(github)
// @grant        none
// ==/UserScript==
(function($) {
	if (typeof $ !== "function" && typeof jQuery !== "function") {
		console.log("jQuery-Extensions-touchJS 缺少jQuery依赖")
		return false;
	}
	$.fn.touch = function(v, fn) {
		// 预处理
		var defFn = function() {
				return false
			},
			fnMap = {
				"def": defFn,
				"left": defFn,
				"right": defFn,
				"top": defFn,
				"down": defFn,
				"tap": defFn,
				"doubleTap": defFn,
				"longPress": defFn,
				"longPressCancel": defFn
			};
		if (typeof v === "string" && typeof fn === "function" && fnMap.hasOwnProperty(v)) {
			fnMap[v] = fn;
		} else if (typeof v === "object" && !fn) {
			fnMap = $.extend({}, fnMap, v);
		}
		// 正式处理
		if (v) {
			var t = $(this),
				i = -1,
				x = -1,
				l = false,
				s = false,
				tn = 0,
				x1, x2, y1, y2;
			t[0].addEventListener('touchstart', ts, false);
			t[0].addEventListener('touchmove', tm, false);
			t[0].addEventListener('touchend', te, false);

			//具体实现
			function init(e, flag) { //初始化
				e = e || window.event
				e.preventDefault();
				clearTimeout(x);
				clearTimeout(i);
				x = -1, i = -1;
				if (flag) {
					l = false;
					s = false;
					tn = 0;
				}
				return e;
			}

			function dir(x1, y1, x2, y2) { //判方向
				if (Math.abs(y2 - y1) < Math.abs(x2 - x1)) {
					if (x2 > x1) {
						return "right"
					} else {
						return "left"
					}
				} else {
					if (y2 > y1) {
						return "down"
					} else {
						return "up"
					}
				}
				return "def"
			}

			function ts(e) { //touchstart
				var e = init(e);
				x1 = e.changedTouches[0].clientX;
				y1 = e.changedTouches[0].clientY;
				i = setTimeout(function() {
					l = true;
					fnMap["longPress"]();
				}, 600)
			}

			function tm(e) { //touchmove
				var e = e || window.event;
				x2 = e.changedTouches[0].clientX;
				y2 = e.changedTouches[0].clientY;
				if (Math.abs(x1 - x2) > 10 || Math.abs(y1 - y2) > 10) {
					init(e);
					s = true;
					fnMap[dir(x1, y1, x2, y2)]()
				}
			}

			function te(e) { //touchend
				var e = init(e);
				if (l) {
					l = false;
					fnMap["longPressCancel"]();
				} else if (!s) {
					tn += 1;
					setTimeout(function() {
						console.log(tn);
						if (tn >= 2) {
							fnMap["doubleTap"]();
						} else if (tn === 1) {
							fnMap["tap"]();
						}
						tn = 0;
					}, 200)
				}
				s=false;
			}
		}
		return $(this)
	}
})(jQuery);