I also think that the touchmove event touchmove fires every time it is clicked. By returning false from this function, you cancel only one move event on the screen and not all subsequent touch move events.
You cannot use the touchend event since you want to call self.customScrollTo(nextStep); as soon as the pointer ran 100px.
You want to prevent the touchmove callback from being executed after the pointer has passed 100px, this can be done in different ways, that is.
- Using a flag variable of type
var trackPointer = true; , check the box every time touchmove starts and sets this flag to false when the pointer has passed 100px. - When the pointer moves 100 pixels, set
startEvent to null and check this variable on touchmove . - Untie the touchmove event when the pointer has traveled 100px.
Note. The touchmove event touchmove attached every time a touchstart fired on this element, these events do not overwrite each other, but add up! Therefore, you may want to bind the event only once (i.e., on the DOM ready) or cancel the event when it is no longer needed.
The latter is probably the simplest and can be performed, i.e. on touchend (use namespaces to not unleash the same events associated with other scripts):
// Unbind all touchmove.myNameSpace events on touchend.myNameSpace. self._$body.bind('touchend.myNameSpace').function (event) { self._$body.unbind('touchmove.myNameSpace'); });
and when the pointer passed 100px:
self.customScrollTo(nextStep); // Unbind all touchmove.myNameSpace events. self._$body.unbind('touchmove.myNameSpace');
Since "touchhend" does not start when the pointer is outside the element (I'm not sure about touchmove ), you can also touchmove right before the binding:
event.preventDefault(); // Unbind all touchmove.myNameSpace events and (re)bind touchmove.myNameSpace event. self._$body.unbind('touchmove.myNameSpace').bind('touchmove.myNameSpace', function (event) { var moveEvent = event.originalEvent.touches[0];
So you can try (I have not tested it):
touch: function () { var self = this; this._$body.bind('touchstart', function (event) { var startEvent = event.originalEvent.touches[0]; event.preventDefault(); self._$body.unbind('touchmove.myNameSpace').bind('touchmove.myNameSpace', function (event) { var moveEvent = event.originalEvent.touches[0]; var diff = { x: startEvent.clientX - moveEvent.clientX, y: startEvent.clientY - moveEvent.clientY }; var nextStep; event.preventDefault();
PS: You might want to use a library such as HammerJS ( https://github.com/hammerjs/hammer.js ) to make gestures work in a cross browser, as well as not touching the device.