In my code, I am using three variables. The first one is x
which serves as the starting point, followed by myCount
which counts the number of swipes a user performs, and finally, dist
which calculates the distance from the initial point.
I want to set myCount
to -1 if the swipe (dragging on mobile) takes the user more than -201 units to the left, indicating a negative movement. While storing positive numbers works correctly for right swipes, it fails to store negative values effectively.
Is there any solution to address this issue?
This is the progress I have made so far:
CodePen$(function stripeAnimeModule() {
// Variables
var screen = $('.page1'),
buttons = $('.buttons').find('.button'),
myCount = 1,
x,
dist;
// Functions
function clicked(e) {
if ($(this).hasClass('prev')) {
myCount -= 1;
console.log(myCount, 'this is clicked');
} else {
myCount += 1;
console.log(myCount);
}
}
function touchStart(e) {
return x = e.touches[0].pageX;
e.preventDefault();
}
function touchMove(e) {
var drag = e.touches[0].pageX;
var dist = Math.sqrt(x + drag);
e.preventDefault();
}
function touchEnd(e) {
var dist = e.changedTouches[0].pageX - x;
if (dist < Math.abs(-200)) {
myCount = myCount;
console.log('nothing', dist, myCount);
} else if (dist > 201) {
myCount += 1;
console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
} else if (dist > -201) {
myCount -= 1;
console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
}
e.stopPropagation();
e.preventDefault();
}
buttons.on({ click: clicked });
screen.bind({ touchstart:touchStart, touchmove:touchMove, touchend: touchEnd });
})
* {
margin: 0;
padding: 0;
}
ul li {
list-style: none;
}
a {
display: block;
}
.page1 {
position: absolute;
width: 100vw;
height: 100vh;
background-color: grey;
}
.buttons {
z-index: 1;
}
.prev {
position: absolute;
left: 0;
margin: 0 40px;
}
.shrink {
position: absolute;
}
.next {
position: absolute;
right: 0;
margin: 0 40px;
}
<div class="page1" href="#"></div>
<ul class="buttons">
<li class="button prev">Prev</li>
<li class="shrink"></li>
<li class="button next">Next</li>
</ul>
<br><br><br><br><br><br><br><br><br><br><br><br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>