add mobile swipe event js

This commit is contained in:
Gerome Matilla 2020-06-04 12:52:56 +08:00
parent 574b7d33df
commit afb1f6d116
2 changed files with 63 additions and 0 deletions

View File

@ -231,5 +231,7 @@
<script src="js/weather-screen.js"></script> <script src="js/weather-screen.js"></script>
<script src="js/weather-settings.js"></script> <script src="js/weather-settings.js"></script>
<script src="js/web-menu.js"></script> <script src="js/web-menu.js"></script>
<script src="js/mobile-swipe-event.js"></script>
</body> </body>
</html> </html>

61
js/mobile-swipe-event.js Normal file
View File

@ -0,0 +1,61 @@
const swipeEvent = (elementID, callback) => {
var el = document.getElementById(elementID);
var dir;
el.addEventListener(
'touchstart',
(event) => {
// event.preventDefault();
xDown = event.touches[0].clientX;
yDown = event.touches[0].clientY;
},
{ passive: true }
);
el.addEventListener(
'touchmove',
(event) => {
// event.preventDefault();
if ( ! xDown || ! yDown ) {
return;
}
var xUp = event.touches[0].clientX;
var yUp = event.touches[0].clientY;
var xDiff = xDown - xUp;
var yDiff = yDown - yUp;
if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {
if ( xDiff > 0 ) {
// Left Swipe
dir = 'left'
} else {
// Right Swipe
dir = 'right';
}
} else {
if ( yDiff > 0 ) {
// Up Swipe
dir = 'up';
} else {
// Down Swipe
dir = 'down';
}
}
/* Reset values */
xDown = null;
yDown = null;
if (dir !== ""){
if (typeof callback == "function") {
callback(el, dir);
}
}
},
{ passive: true }
);
}