SORU
15 ŞUBAT 2010, PAZARTESİ


İPhone ve Android üzerinde JavaScript ile parmak bir tokatlamak tespit

Nasıl bir kullanıcı JavaScript ile bir web sayfası üzerinden bir yöne parmağını çalınmış olduğunu tespit edebilir misiniz?

Eğer iPhone ve Android telefon hem de web siteleri için çalışacak bir çözüm varsa merak ettim.

CEVAP
22 NİSAN 2014, Salı


Basit vanilyalı JS kod örneği:

document.addEventListener('touchstart', handleTouchStart, false);        
document.addEventListener('touchmove', handleTouchMove, false);

var xDown = null;                                                        
var yDown = null;                                                        

function handleTouchStart(evt) {                                         
    xDown = evt.touches[0].clientX;                                      
    yDown = evt.touches[0].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 ) {
            /* left swipe */ 
        } else {
            /* right swipe */
        }                       
    } else {
        if ( yDiff > 0 ) {
            /* up swipe */ 
        } else { 
            /* down swipe */
        }                                                                 
    }
    /* reset values */
    xDown = null;
    yDown = null;                                             
};

Android test.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • merumputdotcom

    merumputdotc

    24 ŞUBAT 2012
  • paikimchung

    paikimchung

    12 Mayıs 2006
  • TechXCentral

    TechXCentral

    12 Temmuz 2011