Touch Swipe Detection in Pure JavaScript

Touch Swipe Detection in Pure JavaScript
Code Snippet:Simple Swipe Gesture Detection
Author: Tommy Hodgins
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 8,086
License: MIT
Edit Code online: View on CodePen
Read More

A simple touchscreen gesture recognition in pure JavaScript for single-cursor swipe up, down, left and right touch swipe detection. It is helpful for swipe to reveal/conceal menus (swipe on e.target) or wipe left/right to control slideshow position.

How to Create Touch Swipe Detection in Pure JavaScript

1. First of all, create the HTML structure as follows:

 <h1>Swipe to Detect Gestures<br><small>(touchscreen only)</small></h1> 
     <div id="surface">
     <h2 id="output">Swipe here</h2>
     </div>

2. After that, add the following CSS styles to your project:

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  font-family: sans-serif;
  color: #1c5706;
  background: #013;
  position: fixed;
  top: 0;
  left: 0;
}
h1, div {
  text-align: center;
  transition: opacity .5s ease-in-out;
}
#surface{
    width: 100%;
    height: 360px;
    background: #f2f2f2;
    border: 1px solid #ccc;
    padding: 20px;
    text-align: center;
    box-sizing: border-box;
    margin: 14px 0;
}

3. Finally, add the following JavaScript code and done.

var gesture = {
      x: [],
      y: [],
      match: ''
    },
    tolerance = 100,
    output = document.getElementById('output');

var surface = document.getElementById('surface');

surface.addEventListener('touchstart',function(e){
  e.preventDefault()
  for (i=0;i<e.touches.length;i++){
    var dot = document.createElement('em');
    dot.id = i
    dot.style.top = e.touches[i].clientY-25+'px'
    dot.style.left = e.touches[i].clientX-25+'px'
    document.body.appendChild(dot)
    gesture.x.push(e.touches[i].clientX)
    gesture.y.push(e.touches[i].clientY)
  }
});
surface.addEventListener('touchmove',function(e){
  e.preventDefault()
  for (var i=0; i<e.touches.length; i++) {
    var dot = document.getElementById(i);
    dot.style.top = e.touches[i].clientY-25+'px'
    dot.style.left = e.touches[i].clientX-25+'px'
    gesture.x.push(e.touches[i].clientX)
    gesture.y.push(e.touches[i].clientY)
  }
});
surface.addEventListener('touchend',function(e){
  var dots = document.querySelectorAll('em'),
      xTravel = gesture.x[gesture.x.length-1] - gesture.x[0],
      yTravel = gesture.y[gesture.y.length-1] - gesture.y[0];
  if (xTravel<tolerance && xTravel>-tolerance && yTravel<-tolerance){
    gesture.match = 'Swiped Up'
  }
  if (xTravel<tolerance && xTravel>-tolerance && yTravel>tolerance){
    gesture.match = 'Swiped Down'
  }
  if (yTravel<tolerance && yTravel>-tolerance && xTravel<-tolerance){
    gesture.match = 'Swiped Left'
  }
  if (yTravel<tolerance && yTravel>-tolerance && xTravel>tolerance){
    gesture.match = 'Swiped Right'
  }
  if (gesture.match!==''){
    output.innerHTML = gesture.match
    output.style.opacity = 1
  }
  setTimeout(function(){
    output.style.opacity = 1
  },500)
  gesture.x = []
  gesture.y = []
  gesture.match = xTravel = yTravel = ''
  for (i=0;i<dots.length;i++){
    dots[i].id = ''
    dots[i].style.opacity = 1
    setTimeout(function(){
      document.body.removeChild(dots[i])
    },1000)
  }
})

That’s all! hopefully, you have successfully integrated this touch swipe detection code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X