jsComponents.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. ?>
  3. <script>
  4. window.onSwipe = function (el, callbackLeft, callbackRight ) {
  5. el.addEventListener('touchstart', handleTouchStart, false);
  6. el.addEventListener('touchmove', handleTouchMove, false);
  7. var xDown = null;
  8. var yDown = null;
  9. function getTouches(evt) {
  10. return evt.touches || // browser API
  11. evt.originalEvent.touches; // jQuery
  12. }
  13. function handleTouchStart(evt) {
  14. const firstTouch = getTouches(evt)[0];
  15. xDown = firstTouch.clientX;
  16. yDown = firstTouch.clientY;
  17. };
  18. function handleTouchMove(evt) {
  19. if ( ! xDown || ! yDown ) {
  20. return;
  21. }
  22. var xUp = evt.touches[0].clientX;
  23. var yUp = evt.touches[0].clientY;
  24. var xDiff = xDown - xUp;
  25. var yDiff = yDown - yUp;
  26. if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
  27. if ( xDiff > 0 ) {
  28. /* right swipe */
  29. callbackRight();
  30. } else {
  31. /* left swipe */
  32. callbackLeft();
  33. }
  34. } else {
  35. if ( yDiff > 0 ) {
  36. /* down swipe */
  37. } else {
  38. /* up swipe */
  39. }
  40. }
  41. /* reset values */
  42. xDown = null;
  43. yDown = null;
  44. };
  45. }
  46. </script>