Home > Web Front-end > JS Tutorial > body text

How to Detect Finger Swipe Direction on Mobile Web Pages with JavaScript (iPhone/Android Compatibility)?

DDD
Release: 2024-10-19 18:50:30
Original
683 people have browsed it

How to Detect Finger Swipe Direction on Mobile Web Pages with JavaScript (iPhone/Android Compatibility)?

Detecting Finger Swipes on Mobile Web Pages

In today's mobile-centric world, it's crucial to provide intuitive and responsive web experiences on all devices. Users expect smooth navigation and interaction on both iPhones and Android phones. One common gesture that's essential for mobile navigation is finger swiping.

Problem: How do you accurately detect a user's finger swipe direction on a web page using JavaScript, ensuring compatibility between iPhone and Android devices?

Solution: Fortunately, modern browsers provide JavaScript APIs that allow developers to track touch events. Here's a simple Vanilla JS code snippet that you can incorporate into your project:

<code class="javascript">document.addEventListener('touchstart', handleTouchStart, false);        
document.addEventListener('touchmove', handleTouchMove, false);

var xDown = null;                                                        
var yDown = null;

function getTouches(evt) {
  return evt.touches ||             // browser API
         evt.originalEvent.touches; // jQuery
}                                                     
                                                                         
function handleTouchStart(evt) {
    const firstTouch = getTouches(evt)[0];                                      
    xDown = firstTouch.clientX;                                      
    yDown = firstTouch.clientY;                                      
};                                                
                                                                         
function handleTouchMove(evt) {
    if ( ! xDown || ! yDown ) {
        return;
    }

    var xUp = evt.touches[0].clientX;                                    
    var yUp = evt.touches[0].clientY;

    var xDiff = xDown - xUp;
    var yDiff = yDown - yUp;
                                                                         
    if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
        if ( xDiff > 0 ) {
            /* right swipe */ 
        } else {
            /* left swipe */
        }                       
    } else {
        if ( yDiff > 0 ) {
            /* down swipe */ 
        } else { 
            /* up swipe */
        }                                                                 
    }
    /* reset values */
    xDown = null;
    yDown = null;                                             
};</code>
Copy after login

Testing: This code snippet has been tested and confirmed to work on Android devices. Implement it into your project to enhance the user experience for mobile users on both iPhone and Android phones.

The above is the detailed content of How to Detect Finger Swipe Direction on Mobile Web Pages with JavaScript (iPhone/Android Compatibility)?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template