双击并单击冲突(jQuery和Hammer.js)

时间:2014-04-28 05:00:46

标签: jquery hammer.js

我正在尝试一些疯狂的东西,我想实现一个Instagram功能:两次点击即可。

所以,我正在使用最新版本的jQuery和Hammer.js。我不是高级专业人士,但我试图编写一个JavaScript代码来识别哪个事件。

var postDoubleTapped;
postDoubleTapped = false;

Hammer($('.post').get(0)).on('doubletap', function(event) {
  event.preventDefault();
  postDoubleTapped = true;

  console.log('Double tap!');
  return false;
});

$(document).on('click', '.post a', function(event) {
  event.preventDefault();
  console.log(postDoubleTapped);

  setTimeout((function(_this) {
    return function() {
      if (!postDoubleTapped) {
        location.href = $(_this).attr('href');
      }
      postDoubleTapped = false;
    };
  })(this), 500);
  return false;
});

正如您在示例(http://codepen.io/caio/pen/vqEjc)上看到的那样,它不起作用。控制台返回:

Console

还有另一个问题,我无法在_blank代码上重现a目标。

这是更好的方法吗?我是正确的吗?我该如何解决?

3 个答案:

答案 0 :(得分:0)

你是正确的方式,但双击就像点击两次。所以你在这两种情况下都使用锤子:

var i=0;
Hammer($('.post').get(0)).on('doubletap', function(event) {
    i++;
    console.log( 'double tap' + i );
});

$(".post a").hammer();
$(".post a").on('tap', function (event) {
    i++;
    console.log( 'single tap' + i );
});

答案 1 :(得分:0)

您可以通过编程方式触发点击,而不是自己处理重定向:

fireEvent(_this, 'click');

在您的点击处理程序中,您可以检查是否以编程方式触发了事件:

$(document).on('click', '.post a', function(event) {

    // ignore the handler when the click is fired programmatically
    if (event.originalEvent.synthetic) {
        return;
    }
    ...

这意味着仍然使用_blank。它确实需要以下函数来在大多数浏览器中触发事件:

/**
 * Fire an event handler to the specified node. Event handlers can detect that the event was fired programatically
 * by testing for a 'synthetic=true' property on the event object
 * @param {HTMLNode} node The node to fire the event handler on.
 * @param {String} eventName The name of the event without the "on" (e.g., "focus")
 */
function fireEvent(node, eventName) {
    // Make sure we use the ownerDocument from the provided node to avoid cross-window problems
    var doc;
    if (node.ownerDocument) {
        doc = node.ownerDocument;
    } else if (node.nodeType == 9){
        // the node may be the document itself, nodeType 9 = DOCUMENT_NODE
        doc = node;
    } else {
        throw new Error("Invalid node passed to fireEvent: " + node.id);
    }

     if (node.dispatchEvent) {
        // Gecko-style approach (now the standard) takes more work
        var eventClass = "";

        // Different events have different event classes.
        // If this switch statement can't map an eventName to an eventClass,
        // the event firing is going to fail.
        switch (eventName) {
            case "click": // Dispatching of 'click' appears to not work correctly in Safari. Use 'mousedown' or 'mouseup' instead.
            case "mousedown":
            case "mouseup":
                eventClass = "MouseEvents";
                break;

            case "focus":
            case "change":
            case "blur":
            case "select":
                eventClass = "HTMLEvents";
                break;

            default:
                throw "fireEvent: Couldn't find an event class for event '" + eventName + "'.";
                break;
        }
        var event = doc.createEvent(eventClass);

        var bubbles = eventName == "change" ? false : true;
        event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.

        event.synthetic = true; // allow detection of synthetic events
        // The second parameter says go ahead with the default action
        node.dispatchEvent(event, true);
    } else  if (node.fireEvent) {
        // IE-old school style
        var event = doc.createEventObject();
        event.synthetic = true; // allow detection of synthetic events
        node.fireEvent("on" + eventName, event);
    }
};

代码中的另一个问题是超时未被取消。我改成了:

if (postDoubleClickTimeout) {
    clearTimeout(postDoubleClickTimeout);
}
postDoubleClickTimeout = setTimeout(...);

现在

请参阅http://codepen.io/anon/pen/fsApG

答案 2 :(得分:0)

尝试($(' .post')。get(0))。off(' doubletap')。on(' doubletap',function(event) ){使用关闭点击。