更新(从答案中添加)
(function($) {
$.fn.lOngpress= function(longCallback, duration) {
// set some defaults
let defaults = {
click: function(el, e) {},
start: function(el, e) { el.css("color", "#000"); },
complete: function(el, e) { el.css("color", "#FFF"); },
cancel: function(el, e) { el.css("color", "#F00"); },
duration: 1000
};
// extend the options
let optiOns= $.extend({}, defaults);
return this.each(function() {
var $this = $(this);
// to keep track of how long something was pressed
var mouse_down_time;
var timeout;
$this.data("clicked", "false");
// mousedown or touchstart callback
function mousedown_callback(e) {
// start callback
options.start($this, e);
$this.data("clicked", "true");
mouse_down_time = new Date().getTime();
var cOntext= $(this);
// set a timeout to call the longpress callback when time elapses
timeout = setTimeout(function() {
if (typeof lOngCallback=== "function") {
longCallback.call(context, e);
options.complete($this, e);
$this.data("clicked", "false");
} else {
$.error('Callback required for long press. You provided: ' + typeof longCallback);
}
}, options.duration);
}
// mouseup or touchend callback
function mouseup_callback(e) {
var press_time = new Date().getTime() - mouse_down_time;
if (press_time
我有一个不久前找到的jQuery插件(想记住源代码),它使我可以在网站上具有长按功能:
(function(b) {
b.fn.lOngpress= function(e, c, d) {
"undefined" === typeof d && (d = 1000);
return this.each(function() {
function g(a) {
h = (new Date).getTime();
var c = b(this);
f = setTimeout(function() {
"function" === typeof e ? e.call(c, a) : b.error("Callback required for long press. You provided: " + typeof e)
}, d)
}
function k(a) {
(new Date).getTime() - h
效果很好,但是我想知道是否有一种方法可以说背景是根据长按的持续时间而定的#fff
,#000
还是制作任何其他自定义动画。
我当时在玩弄,但无法弄清楚。我试图在按住长按的每一秒钟添加一个类,然后为每个添加一个CSS规则。然后,我也尝试steps
在CSS中使用它来增加时间,但由于只有在长按成功加载后才添加类,因此无法使其正常工作。