Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire method only once when using panright with Hammer.js?

I am running into issues with getting Hammer.js (with the jQuery plugin) to function properly when using swiperight. Therefore, I need to use panright.

When panning to the right, my event fires many, many times.

How can this be updated so it only runs once? (then again when the user swipes to the right or left on another slide)

$('.slide').hammer().on('panright', doSomething);
like image 556
Cofey Avatar asked Jan 07 '15 03:01

Cofey


2 Answers

I think you should capture a panend event. For example:

var myElement = document.getElementById('myID');
var mc = new Hammer.Manager(myElement);
mc.add(new Hammer.Pan({direction:Hammer.DIRECTION_HORIZONTAL, threshold:80, pointers: 0}));
mc.on("panend", function(ev) {
  if(ev.direction == Hammer.DIRECTION_RIGHT) 
  {
    //do what you want here
  }
});
like image 92
user1630809 Avatar answered Sep 22 '22 00:09

user1630809


You can filter the event by isFinal property:

var mc = new Hammer($('.screen'));
mc.on("swipeleft", moveHandler);
mc.on("swiperight", moveHandler);

function moveHandler(event){
  if(event.isFinal){
  // do something on the last animation frame
  }
}
like image 38
Giuseppe Caponetto Avatar answered Sep 20 '22 00:09

Giuseppe Caponetto