LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Touch Swipe Direction Detection Using Pure JavaScript

Touch Swipe Direction Detection Using Pure JavaScript

Touch-Swipe-Direction-Detection-Using-Pure-JavaScript
A simple touchscreen gesture recognition in pure JavaScript for single-cursor swipe up, down, left, and right touch swipe detection. It is helpful for swiping to reveal/conceal menus (swipe on e.target) or wipe left/right to control slideshow position. Use the Mobile screen to check this.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To CreateTouch Swipe Direction Detection Using Pure JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
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;
}
</style>

HTML:

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

JavaScript:

<script type="text/javascript">
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)
}
})
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Recommended For You:
ViewPort Height/Width CSS Code: Set You DIV Size As Visitors Screen

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *