Adding swipe actions to a web site.
If you are reading this article on a phone or other touch device, did you know you can swipe left and right to get to other articles? Go on, try it now. I’ll wait.
This turned out to be easy to implement thanks to Hammer.js. Here’s how I did it.
-
I downloaded the Hammer.js library into my JavaScript directory:
wget https://hammerjs.github.io/dist/hammer.min.js
and creates a new file called
swipe.js
in that directory. -
I added the following to the HTML template of my article pages:
<script src="/js/hammer.min.js" defer></script> <script src="/js/swipe.js" defer></script>
This loads the Hammer library and my application code into the web page. As an optimization I add the
defer
attribute so that the script loading does not block the initial page load: after all this is optional stuff that should not slow sown the normal functionality of the page. -
I already had the following navigational structure in the article pages for going to the the previous and next pages:
<nav> <a id="prev" href="...">« ...</a> <a id="next" href="...">... »</a> </nav>
The JavaScript can pluck out the destination URLs from the
href
attributes of the#t
and#prev
elements. -
I added the following to
swipe.js
:const mc = new Hammer(document.body) mc.on('swipe', (ev) => { const dir = (ev.deltaX < 0) ? 'next' : 'prev' const a = document.getElementById(dir) if (a) { window.location = a.href } })
This code initializes Hammer.js and sets up a swipe lister. When a swipe is detected the code looks at the
deltaX
event property to determine if this was a left or right swipe, pulls out the URL from thehref
of the corresponding link, and navigates to that link.
This simple solution works fine, but there is no feedback to the user to show that a swipe has happened. In the next article I’ll show how to add animation to improve the UX.