2012-11-20 17 views
16

आईओएस पर मोबाइल सफारी से क्लिक/टैप विलंब को आप कैसे हटाते हैं?स्पीड फिक्स: jQuery मोबाइल ऐप्स में 300ms विलंब को कैसे निकालें

मैंने घटना श्रोताओं के साथ काफी हद तक झुका दिया है, और बिना किसी खुशी के विभिन्न स्क्रिप्ट्स (जैसे Lightning Touch) का एक गुच्छा इस्तेमाल किया है। ऐसे कुछ समाधान हैं जो काम करते थे, हालांकि इन प्रकार की स्क्रिप्ट आपको डीओएम पर प्रत्येक लिंक पर एक गंतव्य तत्व कोड करने के लिए मजबूर करती हैं। दुर्भाग्य से यह कुछ तेज़ और कुछ धीमी गति से संक्रमण कर सकता है, जो मेरे लिए काम नहीं करेगा।

उत्तर

23

मैं अंत में अथक खोज करने के बाद मेरी गति संकट का जवाब मिल गया है, और यह (this thread, काफी विस्तार में चला जाता है अन्य उपयोगकर्ताओं से टिप्पणी में कुछ बदलाव के साथ) FastClick के रूप में आता है।

FastClick.js स्क्रिप्ट को शामिल करें, ऑनलोड श्रोता जोड़ें, और अपनी <body> सामग्री को एक अवधि में लपेटें, और आपके ऐप को और अधिक देशी महसूस करना शुरू हो जाना चाहिए।


ऑनलोड श्रोता:<body onLoad="initFastButtons();">

स्पैन लपेटें:

<body onLoad="initFastButtons();"> 
    <span id="fastclick"> 

    [...] 

    </span> 
</body> 

FastClick.js

//======================================================== FASTCLICK 
     function FastButton(element, handler) { 
      this.element = element; 
      this.handler = handler; 
      element.addEventListener('touchstart', this, false); 
     }; 
     FastButton.prototype.handleEvent = function(event) { 
      switch (event.type) { 
       case 'touchstart': this.onTouchStart(event); break; 
       case 'touchmove': this.onTouchMove(event); break; 
       case 'touchend': this.onClick(event); break; 
       case 'click': this.onClick(event); break; 
      } 
     }; 
     FastButton.prototype.onTouchStart = function(event) { 

event.stopPropagation(); 
      this.element.addEventListener('touchend', this, false); 
      document.body.addEventListener('touchmove', this, false); 
      this.startX = event.touches[0].clientX; 
      this.startY = event.touches[0].clientY; 
isMoving = false; 
     }; 
     FastButton.prototype.onTouchMove = function(event) { 
      if(Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) { 
       this.reset(); 
      } 
     }; 
     FastButton.prototype.onClick = function(event) { 
      this.reset(); 
      this.handler(event); 
      if(event.type == 'touchend') { 
       preventGhostClick(this.startX, this.startY); 
      } 
     }; 
     FastButton.prototype.reset = function() { 
      this.element.removeEventListener('touchend', this, false); 
      document.body.removeEventListener('touchmove', this, false); 
     }; 
     function preventGhostClick(x, y) { 
      coordinates.push(x, y); 
      window.setTimeout(gpop, 2500); 
     }; 
     function gpop() { 
      coordinates.splice(0, 2); 
     }; 
     function gonClick(event) { 
      for(var i = 0; i < coordinates.length; i += 2) { 
       var x = coordinates[i]; 
       var y = coordinates[i + 1]; 
       if(Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { 
        event.stopPropagation(); 
        event.preventDefault(); 
       } 
      } 
     }; 
     document.addEventListener('click', gonClick, true); 
     var coordinates = []; 
     function initFastButtons() { 
new FastButton(document.getElementById("fastclick"), goSomewhere); 
     }; 
     function goSomewhere() { 
var theTarget = document.elementFromPoint(this.startX, this.startY); 
if(theTarget.nodeType == 3) theTarget = theTarget.parentNode; 

var theEvent = document.createEvent('MouseEvents'); 
theEvent.initEvent('click', true, true); 
theTarget.dispatchEvent(theEvent); 
     }; 
//======================================================== 
+2

किसी को भी एक गैर js प्रधान समाधान का सामना करना पड़ा है? शायद सीएसएस सूचक-घटनाओं का उपयोग: कोई भी नहीं; ? – JackMahoney

+1

फास्टक्लिक ** पूरी तरह से ** jquery draggable के साथ विफल रहता है – 8DK

0

यह मेरे लिए काम कर रहा है। जब भी कोई नया पेज डोम में जोड़ा जाता है, हम तो सभी लिंक के लिए तेजी से क्लिक संलग्न कर सकते हैं ...

// when new pages are loaded into the DOM via JQM AJAX Nav, apply ko bindings to that page's DOM 
$(document).on('pageinit', '.ui-page', function (event, data) 
{ 
    var activePage = $(event.target).get(0); 
    FastClick.attach(activePage); 
}); 
0

Ben Howdle हाल ही में एक ओपन सोर्स प्रोजेक्ट Touche.js जो एक काफी सरल और सुरुचिपूर्ण तरीके से इस समस्या को संबोधित बनाया। इस समस्या के समाधान की तलाश में किसी के लिए एक लायक हो सकता है।

0

मैं सीधे माइकज़ सिफारिश पर टिप्पणी पोस्ट करने में सक्षम नहीं हूं।

मैंने इसे सफलतापूर्वक उपयोग किया, लेकिन विशेष रूप से एंड्रॉइड उपकरणों के लिए कुछ अतिरिक्त tweek करना पड़ा।

इसके बजाय gonClick में event.preventDefault(); बुलाने की

(), मैं भी event.stopImmediatePropagation();

कॉल करने के लिए नहीं तो आप मुसीबत में चला सकता है विशेष रूप से, जब इस तरह के पैनल के रूप में गतिशील तत्वों,, तत्व के शीर्ष पर खोल रहे हैं आप क्लिक किया था।

पूरा gonClick() विधि MikeZ समाधान के साथ प्रयोग की जाने वाली:

function gonClick(event) { 
    for (var i = 0; i < coordinates.length; i += 2) { 
     var x = coordinates[i]; 
     var y = coordinates[i + 1]; 
     if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { 
      event.stopPropagation(); 
      event.preventDefault(); 
      event.stopImmediatePropagation(); 
     } 
    } 
};