jQuery-Extensions-touchJS

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

Version vom 08.11.2022. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greatest.deepsurf.us/scripts/454450/1114710/jQuery-Extensions-touchJS.js

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         jQuery-Extensions-touchJS
// @namespace    http://tampermonkey.net/
// @version      1.0
// @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,
				"longPress": 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,
				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) { //初始化
				if (flag !== false) {
					e = e || window.event
					e.preventDefault();
				}
				clearTimeout(x);
				clearTimeout(i);
				x = -1, i = -1;
				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;
				console.log("ts " + x1 + "," + y1)
				i = setTimeout(function() {
					i = -1;
					fnMap["longPress"]();
				}, 600)
			}

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

				// x = setTimeout(function() {
				// 	fnMap[dir(x1, y1, x2, y2)]
				// }, 200);
			}

			function te(e) { //touchend
				console.log("te")
				var e = e || window.event;
				e.preventDefault();
				if (i >= 0) {
					fnMap["tap"]();
				}
				init(e, false)
			}
		}
		return $(this)
	}
})(jQuery);