HOW TO: remove the 300ms delay when clicking on a link in JQuery Mobile

来源:互联网 发布:软件静默安装器 编辑:程序博客网 时间:2024/05/11 01:59
文章来源:http://forum.jquery.com/topic/how-to-remove-the-300ms-delay-when-clicking-on-a-link-in-jquery-mobile
Here is how to remove the annoying click delay on your jquery mobile website.


Create a file called fastclick.js and paste following code in it:
Copy code
  1. //======================================================== FASTCLICK
  2.          function FastButton(element, handler) {
  3.             this.element = element;
  4.             this.handler = handler;
  5.             element.addEventListener('touchstart', this, false);
  6.          };
  7.          FastButton.prototype.handleEvent = function(event) {
  8.             switch (event.type) {
  9.                case 'touchstart': this.onTouchStart(event); break;
  10.                case 'touchmove': this.onTouchMove(event); break;
  11.                case 'touchend': this.onClick(event); break;
  12.                case 'click': this.onClick(event); break;
  13.             }
  14.          };
  15.          FastButton.prototype.onTouchStart = function(event) {
  16.              event.stopPropagation();
  17.             this.element.addEventListener('touchend', this, false);
  18.             document.body.addEventListener('touchmove', this, false);
  19.             this.startX = event.touches[0].clientX;
  20.             this.startY = event.touches[0].clientY;
  21. isMoving = false;
  22.          };
  23.          FastButton.prototype.onTouchMove = function(event) {
  24.             if(Math.abs(event.touches[0].clientX - this.startX) > 10 || Math.abs(event.touches[0].clientY - this.startY) > 10) {
  25.                this.reset();
  26.             }
  27.          };
  28.          FastButton.prototype.onClick = function(event) {
  29.             this.reset();
  30.             this.handler(event);
  31.             if(event.type == 'touchend') {
  32.                preventGhostClick(this.startX, this.startY);
  33.             }
  34.          };
  35.          FastButton.prototype.reset = function() {
  36.             this.element.removeEventListener('touchend', this, false);
  37.             document.body.removeEventListener('touchmove', this, false);
  38.          };
  39.          function preventGhostClick(x, y) {
  40.             coordinates.push(x, y);
  41.             window.setTimeout(gpop, 2500);
  42.          };
  43.          function gpop() {
  44.             coordinates.splice(0, 2);
  45.          };
  46.          function gonClick(event) {
  47.             for(var i = 0; i < coordinates.length; i += 2) {
  48.                var x = coordinates[i];
  49.                var y = coordinates[i + 1];
  50.                if(Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) {
  51.                   event.stopPropagation();
  52.                   event.preventDefault();
  53.                }
  54.             }
  55.          };
  56.          document.addEventListener('click', gonClick, true);
  57.          var coordinates = [];
  58.          function initFastButtons() {
  59. new FastButton(document.getElementById("fastclick"), goSomewhere);
  60.          };
  61.          function goSomewhere() {
  62. var theTarget = document.elementFromPoint(this.startX, this.startY);
  63. if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;
  64. var theEvent = document.createEvent('MouseEvents');
  65. theEvent.initEvent('click', true, true);
  66. theTarget.dispatchEvent(theEvent);
  67.          };
  68. //========================================================

Now in your jquery mobile index.html file, put the following code in the head:

Copy code
  1. <head>
  2. <!-- your css links go here -->
  3. <script src="scripts/jquery-1.6.4.min.js"></script>
  4. <script src="scripts/jquery.mobile-1.0.1.min.js"></script>
  5. <script src="scripts/phonegap-1.3.0.js"></script> <!-- for us phonegap users -->
  6. <script src="scripts/fastclick.js"></script>
  7. </head> 

Now run the script on body load or, preferably, in your pageInit():
Copy code
  1. <body onload="initFastButtons();"> 

Finally, wrap everything in between your body tags with a span called "fastclick". Note that if you use a div, you'll run into problems with input fields... so use a span.

Copy code
  1. <body onload"initFastButtons();">
  2. <span id="fastclick">
  3. <div data-role="page" id="one" style="overflow-x:hidden;">
  4. <div data-role="header" data-id="myheader" data-position="inline">
  5. ......


  6. </span> <!-- end fastclick -->


Done! Clicking/tapping through your app should now feel very responsive.

Original code from this thread: https://forum.jquery.com/topic/google-s-fastbutton
0 0
原创粉丝点击