1

I'm just trying to pick up on some better JavaScript skills and have created a very basic website that randomizes from an array of images or gifs.

The main page just has the gif or image that loads in and then when clicked or tapped the image randomizes from an array of available images.

What I am trying to do is when it is on mobile devices, when the user swipes right or left the browser will interpret this swipe as a 'click' and trigger the function to randomize.

If anyone can tell me how I can get this to work that would be great!!

Thanks in advance

h0bb5
  • 609
  • 1
  • 7
  • 22
  • possible duplicate of [Detect a finger swipe through JavaScript on the iPhone and Android](http://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android) – Lucky Jan 06 '15 at 07:13

2 Answers2

1

Vary old question but replaying so can help to others like me how wanted vary small simple code to get swap event in JavaScript without any dependency. Hope it will help others.

See script at swipe.js

simple to use in html markup :

    <div id="touchsurface1" class="touchsurface" onSwap="touchsurfaceSwap(event)"></div>

Use in Javascript :

    touchsurface = document.getElementById('touchsurface2');
    touchsurface.addEventListener("swap", function(event){alert('Swaped ' + event.detail.direction + ' at ' + event.target.id);}, false);

Demo page : https://vikylp.github.io/swipe/

Vikram Mali
  • 125
  • 1
  • 2
  • 8
0

Use jquery and it has mobile events like on touchstart, swipe etc.

$("p").on("swipe",function(){
  $("span").text("Swipe detected!");
});

Read this documentation for more help

http://www.w3schools.com/jquerymobile/jquerymobile_events_touch.asp

Hoja
  • 1,057
  • 1
  • 12
  • 32