3
I’m trying something different, I would like to implement something similar to an Instagram feature: two tapas to give like.
To try this, I am using the latest versions of jQuery and Hammer.js. I am not an advanced programmer in this area, however I have come to the following logic to identify which event the user wants to run:
var postDoubleTapped;
postDoubleTapped = false;
Hammer($('.post').get(0)).on('doubletap', function() {
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;
});
As you can see in the example (http://codepen.io/caio/pen/vqEjc), didn’t work! The console
returns:
There’s another problem, I can’t reproduce the target="_blank"
on the tag a
.
Is this the best method to do this? Am I on the right track? How can I fix this?
Additional information: that question on Soen suggests using
window.stop
in theondblclick
to stop page switching, but I believe it only works (if it works) on the current page. That other proposes an alternative, however as the behavior is similar to that of a "pop under" (i.e. more used "pro mal" than "pro bem") many browsers do not allow it (in Chrome, for example, it did not work). Finally, here it is an argument discouraging such use.– mgibsonbr
Thank you so much for your help, I am considering all your observations and trying to rebuild my code (stopping using Hammer.js may even be an option). In item 2 you said about the
target
ofHammer
. This does not happen because they are different selectors in events?– Caio Tarifa
@Caiotarifa I don’t understand enough DOM events to know why this happens (because the
$('.post').get(0)
on the Hammer selector also refers to the hyperlink), but I know it shouldn’t matter: events onimg
propagate proa
, propagating pro.post
until you reach thedocument
(where he is treated byon
jQuery). This all occurs "behind the scenes" - independent of the framework/library - so I was rather surprised when thestopPropagation
didn’t work...– mgibsonbr