2010-04-23 10 views
20

तो, मैं एक प्लगइन की क्षमता को लागू करने के लिए देख रहा हूं जिसे मैंने एक स्पर्श-सक्षम इंटरनेट डिवाइस, जैसे आईफोन, आईपैड या एंड्रॉइड से "स्वाइप" स्पर्श करने के लिए लिखा था।स्टैंडअलोन jQuery "स्पर्श" विधि?

क्या वहां कुछ भी है? मैं jQtouch के रूप में पूर्ण कुछ नहीं ढूंढ रहा हूं, हालांकि रिवर्स इंजीनियरिंग पर विचार कर रहा था, जिस कोड को मुझे इसकी आवश्यकता होगी।

इस तक पहुंचने का सबसे अच्छा तरीका कोई सुझाव? कोड का एक स्निपेट पहले से ही उपलब्ध है?

परिशिष्ट: मुझे पता है कि समाधान में सख्ती से jQuery नहीं होगा, क्योंकि मुझे पूरा यकीन है कि इसे संभालने के लिए कोई अंतर्निहित विधियां नहीं हैं। मैं मानक जावास्क्रिप्ट को जवाब में खुद को ढूंढने की अपेक्षा करता हूं।

उत्तर

32
(function($) { 
$.fn.swipe = function(options) { 
    // Default thresholds & swipe functions 
    var defaults = { 
     threshold: { 
      x: 30, 
      y: 10 
     }, 
     swipeLeft: function() { alert('swiped left') }, 
     swipeRight: function() { alert('swiped right') }, 
     preventDefaultEvents: true 
    }; 

    var options = $.extend(defaults, options); 

    if (!this) return false; 

    return this.each(function() { 

     var me = $(this) 

     // Private variables for each element 
     var originalCoord = { x: 0, y: 0 } 
     var finalCoord = { x: 0, y: 0 } 

     // Screen touched, store the original coordinate 
     function touchStart(event) { 
      console.log('Starting swipe gesture...') 
      originalCoord.x = event.targetTouches[0].pageX 
      originalCoord.y = event.targetTouches[0].pageY 
     } 

     // Store coordinates as finger is swiping 
     function touchMove(event) { 
      if (defaults.preventDefaultEvents) 
       event.preventDefault(); 
      finalCoord.x = event.targetTouches[0].pageX // Updated X,Y coordinates 
      finalCoord.y = event.targetTouches[0].pageY 
     } 

     // Done Swiping 
     // Swipe should only be on X axis, ignore if swipe on Y axis 
     // Calculate if the swipe was left or right 
     function touchEnd(event) { 
      console.log('Ending swipe gesture...') 
      var changeY = originalCoord.y - finalCoord.y 
      if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) { 
       changeX = originalCoord.x - finalCoord.x 

       if(changeX > defaults.threshold.x) { 
        defaults.swipeLeft() 
       } 
       if(changeX < (defaults.threshold.x*-1)) { 
        defaults.swipeRight() 
       } 
      } 
     } 

     // Swipe was canceled 
     function touchCancel(event) { 
      console.log('Canceling swipe gesture...') 
     } 

     // Add gestures to all swipable areas 
     this.addEventListener("touchstart", touchStart, false); 
     this.addEventListener("touchmove", touchMove, false); 
     this.addEventListener("touchend", touchEnd, false); 
     this.addEventListener("touchcancel", touchCancel, false); 

    }); 
}; 
})(jQuery); 


$('.swipe').swipe({ 
swipeLeft: function() { $('#someDiv').fadeIn() }, 
swipeRight: function() { $('#someDiv').fadeOut() }, 
}) 

और यह कैसे आप iphone

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { 
    if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "path to iphone page"; 
} 
+0

बहुत बढ़िया! मैं इसे आजमाने के लिए जा रहा हूं और फिर चुने हुए उत्तर के साथ उम्मीद को पोस्ट कर रहा हूं। – dclowd9901

+0

शानदार ढंग से काम किया। मैंने इसे थोड़ा सा बदल दिया ताकि इसे मेरी प्लगइन के माध्यम से शुरू किया जा सके, लेकिन यह कोड का एक बड़ा हिस्सा है। बहुत धन्यवाद! – dclowd9901

+0

jQuery के लिए स्वाइप प्लगइन यहां पाया जा सकता है http://plugins.jquery.com/project/swipe –

संबंधित मुद्दे