0

I'm trying to implement hammer.js to swipe pages (like a book) and I did it. The problem is that this works

  var idHammer1 = document.getElementById("pageHoja1")
  //var hammertime = new Hammer(myElement, hammerOptionsPan);
  var objHammer1 = new Hammer(idHammer1);
  objHammer1.on('panleft panright', function(ev)
  {
    //DBLog("obj1 - gSceneActual Antes: " + gSceneActual + " // X: " + ev.center.x + " Y: " + ev.center.y);
    if (ev.type==='panleft')
    {
      if (!(gSceneActual===2))
      {
        gSceneActual = 2;
        $(":mobile-pagecontainer").pagecontainer("change", "#pageHoja2", { transition: "slide", reverse: false});
      }
    }
    else if (ev.type==='panright')
    {
    }
  });

but this doesn't:

var fSwipe1 = function(ev)
  {
    //DBLog("obj1 - gSceneActual Antes: " + gSceneActual + " // X: " + ev.center.x + " Y: " + ev.center.y);
    if (ev.type==='panleft')
    {
      if (!(gSceneActual===2))
      {
        gSceneActual = 2;
        $(":mobile-pagecontainer").pagecontainer("change", "#pageHoja2", { transition: "slide", reverse: false});
      }
    }
    else if (ev.type==='panright')
    {
    }
  }

  var idHammer1 = document.getElementById("pageHoja1")
  //var hammertime = new Hammer(myElement, hammerOptionsPan);
  var objHammer1 = new Hammer(idHammer1);
  objHammer1.on('panleft panright', fSwipe1(ev))

and this also don't work

function fSwipe1(ev)
  {
    //DBLog("obj1 - gSceneActual Antes: " + gSceneActual + " // X: " + ev.center.x + " Y: " + ev.center.y);
    if (ev.type==='panleft')
    {
      if (!(gSceneActual===2))
      {
        gSceneActual = 2;
        $(":mobile-pagecontainer").pagecontainer("change", "#pageHoja2", { transition: "slide", reverse: false});
      }
    }
    else if (ev.type==='panright')
    {
    }
  }

and since I need to add this event to many pages (variable #) I cant hardcode it... How can I make it variable inside a cycle?

Thanks!

2
  • 1
    Are you getting a javascript error? Which part is not working?
    – KJ Price
    Commented Nov 11, 2014 at 17:08
  • If I do the first one it works perfect (swipe and everything!) but the 2nd and the 3rd don't render the screen at all (I think because there is an error and the whole thing stops). I forgot to tell that it's PhoneGap so... almost no debug possible Commented Nov 11, 2014 at 17:18

1 Answer 1

1

Ah, without knowing the extent of the errors, I do see:

objHammer1.on('panleft panright', fSwipe1(ev));

Here, you are rendering the function automatically, but what you actually want is to use a closure so that the function does not get rendered until the event gets hit. I'm not sure what ev represents, but if it is the event object, then this should work:

objHammer1.on('panleft panright', fSwipe1);

Where all you are doing is passing in the function that you want to be the callback and the even will automatically call this function and pass the event object as the first parameter.

A few other things that I notice:

  1. make sure that you include the javascript library for Hammer
  2. Make sure that gSceneActual is defined before it is evaluated at gSceneActual===2
  3. Make sure that jQuery library is included
3
  • thanks for responding... the 123 points are checked. I tried taking out the "(ev)" but to not avail... ;( and yes! the ev means event... Commented Nov 11, 2014 at 19:31
  • Be sure that you remove the parenthesis "()", those actually call the function and you do not want the function called. You want to pass the function so that it will be called by the event handler.
    – KJ Price
    Commented Nov 11, 2014 at 19:35
  • 1
    OK. I fixed it. I was declaring the var=function outside of the function (in the "global" area) and (don't know exactly why) it didn't work. Now I put it inside the function that calls it and worked. Thanks! Commented Nov 11, 2014 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.