ifightcrime/touch-scroll — Issue #4: getPosition returns NaN
Author: nvitas
Created: 2012-02-28 17:57:04+00:00
State: open

Thanks for implementing the horizontal scrolling I really appreciate it. It works great and I'll be using it in an iPad app I'm currently writing.

When calling the getPosition function currently it returns a NaN. The problem is that an object is being multiplied by -1

i.e. this.getPosition() returns an object {x:0,y:0} and then it is multiplied by -1

getPosition: function() {
var a = [];
this.each(function() {
a.push(-this.getPosition()); <-- problem here
});
return a;
},

I fiddled around with it for a little bit and a simple fix is to get the object first and then multiply each value inside it by -1.

Here's the code:

getPosition: function() {
var a;
this.each(function() {
a = this.getPosition();
a.x = -a.x;
a.y = -a.y;
});
return a;
},

This way when you call:

var position = $('nav').touchScroll('getPosition');

then you can get both the vertical and horizontal values by saying

position.x // for horizontal
position.y // for vertical

Again, thanks for making the horizontal version of this plugin public and hopefully the code above helps make it even better.

Nik