## Thursday, October 6, 2011

I spent some time trying to figure out how to add mobile swipe support without using jQuery Mobile, because the swipe is all I really needed. I don't own an iPhone/iPad/iAnything, so it took a bit of back-and-forth testing - thanks to all the folks over at CSS-Tricks.com - to get this to finally work.

Here is the complete code:

var maxTime = 1000, // allow movement if < 1000 ms (1 sec)
maxDistance = 50,  // swipe movement of 50 pixels triggers the swipe

target = $('#box'), startX = 0, startTime = 0, touch = "ontouchend" in document, startEvent = (touch) ? 'touchstart' : 'mousedown', moveEvent = (touch) ? 'touchmove' : 'mousemove', endEvent = (touch) ? 'touchend' : 'mouseup'; target .bind(startEvent, function(e){ // prevent image drag (Firefox) e.preventDefault(); startTime = e.timeStamp; startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX; }) .bind(endEvent, function(e){ startTime = 0; startX = 0; }) .bind(moveEvent, function(e){ e.preventDefault(); var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX, currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX), // allow if movement < 1 sec currentTime = e.timeStamp; if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) { if (currentX < startX) { // swipe left code here target.find('h1').html('Swipe Left').fadeIn(); setTimeout(function(){ target.find('h1').fadeOut(); }, 1000); } if (currentX > startX) { // swipe right code here target.find('h1').html('Swipe Right').fadeIn(); setTimeout(function(){ target.find('h1').fadeOut(); }, 1000); } startTime = 0; startX = 0; } });  Now, lets break down what each line does. First, some definitions: var maxTime = 1000, // allow movement if < 1000 ms (1 sec) maxDistance = 50, // swipe movement of 50 pixels triggers the swipe target =$('#box'),
startX = 0,
startTime = 0,


The "maxTime" variable is a set to 1000 milliseconds. It is used to compare the touch start and touch end times, and if less than one second (1000 ms) it is considered to be a valid swipe, otherwise it is ignored. Basically, we don't like them slow swipers! Hehe, ok, actually, it to determine if the event ends up being a touch or a swipe.

"maxDistance" is the number of pixels from the touch start point that the mouse or a finger moved to determine if a swipe or touch is occurring.

The "target" variable, is the target element to add swipe support to. For the demo, I added an H1 tag inside of this element to add a swipe message. The "startX" and "startTime" variables are set to zero because the "touchmove" or "mousemove" event looks to see if they are set with the "startX" position and start time. If zero, it ignores the move.

In the next part we figure out of the device has touch support and set up the event names appropriately:

touch = "ontouchend" in document,
startEvent = (touch) ? 'touchstart' : 'mousedown',
moveEvent = (touch) ? 'touchmove' : 'mousemove',
endEvent = (touch) ? 'touchend' : 'mouseup';


The "touch" varible is true if the "ontouchend" event exists, which means touch is supported. All that line is doing is looking to see if that object exists.

The next variables, "startEvent", "moveEvent" and "endEvent" contain the necessary event names for start, move and end, respectively. Which the next lines of code will now bind to:
target
.bind(startEvent, function(e){
// prevent image drag (Firefox)
e.preventDefault();
startTime = e.timeStamp;
startX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX;
})
The "touchstart" or "mousedown" event code contains the following:

"e.preventDefault()" prevents the image drag function in Firefox which actually makes you think you're dragging the image instead of swiping. Now we define "startTime" and "startX" as the start time and position of the swipe. The "startX" needs to get the position of the touch from the touches variable, but only the first one. We don't want to swipe if there are multiple touches (gestures) but it shouldn't work anyway since we're not binding to the gestures event.
.bind(endEvent, function(e){
startTime = 0;
startX = 0;
})
Now the "touchend" or "mouseup" event code only clears the start time and position variables. The reason for this, which was a fun and painful lesson, is that these events don't fire when your mouse/finger leaves the element or goes off the screen. So the actual swipe calculations are done in the move event functions.

.bind(moveEvent, function(e){
e.preventDefault();
var currentX = e.originalEvent.touches ? e.originalEvent.touches[0].pageX : e.pageX,
currentDistance = (startX === 0) ? 0 : Math.abs(currentX - startX),
// allow if movement < 1 sec
currentTime = e.timeStamp;
if (startTime !== 0 && currentTime - startTime < maxTime && currentDistance > maxDistance) {
if (currentX < startX) {
// swipe left code here
setTimeout(function(){
}, 1000);
}
if (currentX > startX) {
// swipe right code here
setTimeout(function(){
}, 1000);
}
startTime = 0;
startX = 0;
}
});

The "touchmove" or "mousemove" events is where the main portion of this swipe code exists. Again, we want to prevent the default functionality of the move/drag. The "currentX" variable is set with the current x position of the finger/mouse. Again, the x position is obtained from the touches variable, if available. The difference "currentDistance" between the starting x and current x position is then determined. We need the absolute value of this number (ignore the negative sign if it is there) to see if it is more than the "maxDistance" variable. Also here is another trick to make sure the move event is occurring after a start event, check to make sure the starting x position isn't zero. If it is, set the "currentDistance" variable to zero. We also need determine the "currentTime" current time of the move event.

Now, we check to make sure the move event occurred after a start event. This can happen is the start event occurs outside of the element or window. So, first we make sure the "startTime" isn't zero, then we check that the time difference from start to current time is less than one second. And finally, we check that the current distance is more than the max distance setting. If it is, then we have a swipe!

YAY finally a swipe! Now to just figure out if it was going to the right or left. We do this by comparing the current position "currentX" to the starting position "startX"; if the current position is less than the start, then it's a swipe to the left and if it is greater, then the swipe was to the right.

Last but not least, we set the start time and position back to zero to prevent multiple firing of the swipe event.

I hope everything had a clear enough explanation. Please feel free to leave a comment or email me (gmail with wowmotty as the user) with questions or if you need further clarification.

1. Hi Mottie, I think this is a fantastic piece of code, well done for working it out. A question I do have though, is it possible to still allow the page to scroll up and down if the user swipes vertically over the anything slider? I've noticed that because the swipe gesture is being captured, it is over-riding the devices default vertical swipe of scrolling the whole document.

2. Hi Angus!

Actually it is possible (demo). Just comment out the two "e.preventDefault();" lines in the code. But then a new issue comes to light, swiping to the left or right when the page is wider than the screen will scroll the screen left/right instead of registering as a swipe in the slider. Hmmm, I guess I could look for two touches, one being the anchor to prevent moving the page...

3. Hi Mottie, thanks for the reply. I've continued with a response on the Anything Slider's GitHub page.

4. Hi Mottie

Thanks a million for this script. I'm new in mobile app development and this script is exactly what I needed. I have one problem though, it seems to only work on js fiddle but not on any of my browsers (latest Chrome & FireFox). Is the something I'm missing here?

Thanks

1. Hi Thando! Are you including jQuery? If you have included it, then open up the Chrome Developer Tools (press F12), then look in the console tab for any errors.

5. Hi Mottie,
Thanks you very much for this script. But one problem I found that if content has data-scroll property to 'y' in HTML then right\left swipe doesn't work. If I remove the data-scroll property, it works perfectly. Do I need to do something for this??

Thanks

1. This code was really meant for a quick and easy way to add swipe support to pages that don't use jQuery Mobile. If you are using jQuery mobile, as I'm assuming that's what you mean by having content with a data-scroll="y", then swipe events already exist so there is no need for this script.

If that isn't what you mean, then could you clarify what script uses data-scroll as I haven't spent a lot of time working with mobile devices.

6. Thank you very much! I try to make it with rubbery effect, Thank you

7. I’m not sure where you’re getting your info about iPad Development, but great topic. I needs to spend some time learning much more or understanding more. Thanks for excellent info I was looking for this information for my mission.

8. Hello,
first time I read this article. I like it - looks really good. When u add the event listener "startEvent" you can delete the line "e.preventDefault();", because u have no movement in there and add that comment over it to the "moveEvent". Maybe u write as comment, that it prevents the browser from doing its default things (scroll, zoom, drag&drop) that readers don´t get confused and think they only have to implement that line to prevent image drag in Firefox.

Greetings Lars

1. My fault. I should have tested it first. Everything is fine. U also need to have that there for mousemove. So, u can let it inside. :)