REMOVE ONCLICK DELAY ON WEBKIT FOR IPHONE

Developing on the webkit for iPhone I encountered a curious delay ononClick events. It seems that the click is triggered with about 300 milliseconds delay. While this is unnoticeable on a standard web page, it can be annoying on a web application. Fortunately the click event can be overridden thus eliminating the delay.

I assume that 300ms is the time frame Apple guesses an user needed to perform gestures, but there are situations where this delay can be really annoying. Think to a calculator application with 300ms delay each time you press a button. Unacceptable.

The simplest solution is to use onTouchStart instead of onClick events. Something like <div ontouchstart="doSomething()"> is perfectly logical and overrides the onClick delay. But the action is triggered as soon as you touch the screen and may end up to undesirable results, so I tried to recreate the mouseDown/mouseUp events sequence with touchStart/touchMove/touchEnd.

Point your iPhone or simulator to my demo page. Clicking on the first button the standard click event is fired (with infamous 300ms delay), the second button instead overrides the onClick event and the action is actually cast on touchEnd with no delay.

The code I use is the following:

function NoClickDelay(el) {
	this.element = el;
	if( window.Touch ) this.element.addEventListener(‘touchstart‘, this, false);
}

NoClickDelay.prototype = {
	handleEvent: function(e) {
		switch(e.type) {
			case ‘touchstart‘: this.onTouchStart(e); break;
			case ‘touchmove‘: this.onTouchMove(e); break;
			case ‘touchend‘: this.onTouchEnd(e); break;
		}
	},

	onTouchStart: function(e) {
		e.preventDefault();
		this.moved = false;

		this.element.addEventListener(‘touchmove‘, this, false);
		this.element.addEventListener(‘touchend‘, this, false);
	},

	onTouchMove: function(e) {
		this.moved = true;
	},

	onTouchEnd: function(e) {
		this.element.removeEventListener(‘touchmove‘, this, false);
		this.element.removeEventListener(‘touchend‘, this, false);

		if( !this.moved ) {
			// Place your code here or use the click simulation below
			var theTarget = document.elementFromPoint(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
			if(theTarget.nodeType == 3) theTarget = theTarget.parentNode;

			var theEvent = document.createEvent(‘MouseEvents‘);
			theEvent.initEvent(‘click‘, true, true);
			theTarget.dispatchEvent(theEvent);
		}
	}
};

The script creates a touchStart event and performs the click action on touchEnd which occurs 300ms before the standard click event. This is just an example to get you started, my function triggers the click event on touchEnd so you still need to add an onClick event (or an Anchor) somewhere if you want something to happen. You could better place directly your code on touchEnd but if you use my method your application will be compatible with both touch (the iphone) and non-touch enabled devices (the standard browser).

To activate the script all you need to do is: new NoClickDelay(document.getElementById(‘element‘));. From now on all your clicks inside the element will be performed with no delay.

Note that you don’t need to apply the NoClickDelay() function to all the objects in the page, but just to a container. If for instance you have an unordered list, you don’t need to add the script to each <li>elements, but just to the <ul>. This has been done to reduce the number of event listeners so less resources are needed.

To closely mimic the standard UI you could add a hover class on touchStart to highlight the pressed object in someway and remove it on touchMove. (Apple places a gray rectangle over pressed elements).

Update 2009/02/27: By popular demand here follows the code that assigns the “pressed” CSS class to the clicked element.

function NoClickDelay(el) {
	this.element = typeof el == ‘object‘ ? el : document.getElementById(el);
	if( window.Touch ) this.element.addEventListener(‘touchstart‘, this, false);
}

NoClickDelay.prototype = {
	handleEvent: function(e) {
		switch(e.type) {
			case ‘touchstart‘: this.onTouchStart(e); break;
			case ‘touchmove‘: this.onTouchMove(e); break;
			case ‘touchend‘: this.onTouchEnd(e); break;
		}
	},

	onTouchStart: function(e) {
		e.preventDefault();
		this.moved = false;

		this.theTarget = document.elementFromPoint(e.targetTouches[0].clientX, e.targetTouches[0].clientY);
		if(this.theTarget.nodeType == 3) this.theTarget = theTarget.parentNode;
		this.theTarget.className+= ‘ pressed‘;

		this.element.addEventListener(‘touchmove‘, this, false);
		this.element.addEventListener(‘touchend‘, this, false);
	},

	onTouchMove: function(e) {
		this.moved = true;
		this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, ‘‘);
	},

	onTouchEnd: function(e) {
		this.element.removeEventListener(‘touchmove‘, this, false);
		this.element.removeEventListener(‘touchend‘, this, false);

		if( !this.moved && this.theTarget ) {
			this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, ‘‘);
			var theEvent = document.createEvent(‘MouseEvents‘);
			theEvent.initEvent(‘click‘, true, true);
			this.theTarget.dispatchEvent(theEvent);
		}

		this.theTarget = undefined;
	}
};

Are you aware of any simpler solution?

/SHARE THE JOY

/REACTIONS

      • AUTHOR: CONSTANTINE MUREEV
      • POSTED ON: 2012/08/20
      • AT: 08:25

      https://github.com/ftlabs/fastclick

      REPLY

      • AUTHOR: LUIS
      • POSTED ON: 2012/08/26
      • AT: 15:06

      hi,

      I’ve tried your code to improve the behavior of the JQM events and the delay which they have after clicking a button element, but I have a problem: after clicking the class ui-btn-active is still on the element and it isn’t removed. I’m not a totally newbie on JQ but, but I’m learning about the events handler, so, I’ll appreciate a little help.

      Thanks in advance!
      Luis Miguel.

      REPLY

    • Great bit of code.

      I modified it a bit to allow the finger to move up to 50 pixels before canceling the click.

      I am confused with .prototype. It seems that somehow handleEvent gets called. How?


      function NoClickDelay(el) {
      this.element = typeof el == ‘object‘ ? el : document.getElementById(el);
      if( window.Touch ) this.element.addEventListener(‘touchstart‘, this, false);
      }

      NoClickDelay.prototype = {
      handleEvent: function(e) {
      switch(e.type) {
      case ‘touchstart‘: this.onTouchStart(e); break;
      case ‘touchmove‘: this.onTouchMove(e); break;
      case ‘touchend‘: this.onTouchEnd(e); break;
      }
      },
      onTouchStart: function(e) {
      e.preventDefault();
      this.moved = false;
      this.x = e.targetTouches[0].clientX;
      this.y = e.targetTouches[0].clientY;
      this.theTarget = document.elementFromPoint(e.targetTouches[0].clientX, e.targetTouches[0].clientY);
      if(this.theTarget.nodeType == 3) this.theTarget = theTarget.parentNode;
      this.theTarget.className+= ‘ pressed‘;
      this.element.addEventListener(‘touchmove‘, this, false);
      this.element.addEventListener(‘touchend‘, this, false);
      },
      onTouchMove: function(e) {
      var x = e.targetTouches[0].clientX;
      var y = e.targetTouches[0].clientY;
      if( Math.sqrt(Math.pow(x-this.x,2)+Math.pow(y-this.y,2))>50){
      this.moved = true;
      this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, ‘‘);
      this.theTarget.className = this.theTarget.className.replace(/ ?active/gi, ‘‘);
      } else {
      if(this.moved==true){
      this.moved=false;
      this.theTarget.className+= ‘ pressed‘;
      }
      }
      },
      onTouchEnd: function(e) {
      this.element.removeEventListener(‘touchmove‘, this, false);
      this.element.removeEventListener(‘touchend‘, this, false);
      if( !this.moved && this.theTarget ) {
      this.theTarget.className = this.theTarget.className.replace(/ ?pressed/gi, ‘‘);
      this.theTarget.className+= ‘ active‘;
      var theEvent = document.createEvent(‘MouseEvents‘);
      theEvent.initEvent(‘click‘, true, true);
      this.theTarget.dispatchEvent(theEvent);
      }
      this.theTarget = undefined;
      }
      };

      REPLY

      • AUTHOR: NIK LINDERS
      • POSTED ON: 2013/01/04
      • AT: 09:32

      Nice script! I made it into a jQuery plug-in and added a function to make the script set focus on the element selected after firing the click event. I found this fixes an issue where input fields are not properly selected on an iPhone if they are within the NoClickDelay element.

      Code:

      (function( $ ) {
      $.fn.noClickDelay = function() {

      var $wrapper = this;
      var $target = this;
      var moved = false;

      $wrapper.bind(‘touchstart mousedown‘,function(e) {
      e.preventDefault();
      moved = false;
      $target = $(e.target);
      if($target.nodeType == 3) {
      $target = $($target.parent());
      }
      $target.addClass(‘pressed‘);

      $wrapper.bind(‘touchmove mousemove‘,function(e) {
      moved = true;
      $target.removeClass(‘pressed‘);
      });

      $wrapper.bind(‘touchend mouseup‘,function(e) {
      $wrapper.unbind(‘mousemove touchmove‘);
      $wrapper.unbind(‘mouseup touchend‘);
      if(!moved && $target.length) {
      $target.removeClass(‘pressed‘);
      $target.trigger(‘click‘);
      $target.focus();
      }
      });
      });

      };
      })( jQuery );

      Use as such:
      $(‘#wrapperElement‘).noClickDelay();

      REPLY

      • AUTHOR: MUNDI
      • POSTED ON: 2013/01/26
      • AT: 22:55

      I my iphone4 I get a click delay of 560ms!!

      What are apple smoking?

      Even with your improvement its still 153ms seconds… better than 560.

      REPLY

      http://cubiq.org/remove-onclick-delay-on-webkit-for-iphone

时间: 2024-08-27 10:09:45

REMOVE ONCLICK DELAY ON WEBKIT FOR IPHONE的相关文章

八大Webkit内核浏览器

列举出时下最流行的Webkit内核浏览器,所以我们并不会做出评测和对比.PS:本文列举的浏览器有一部分为IE+Webkit双核浏览器,如果您对其他IE内核浏览器很感兴趣<抛弃数据!用体验和感觉告诉你什么才是最好用的浏览器!>一文可能会对你有所帮助.尖端技术的Webkit内核WebKit 是一个开源的浏览器引擎,与之相对应的引擎有Gecko(Mozilla Firefox 等使用)和Trident(也称MSHTML,IE 使用).同时WebKit 也是苹果Mac OS X 系统引擎框架版本的名称

SqLiter

1.去重 select *  from daydata where wtid||rectime in (select wtid||rectime from daydata group by wtid||rectime having count(wtid||rectime) > 1) and ctid not in (select min(ctid) from daydata group by wtid||rectime     having count(wtid||rectime)>1) (适

运维工单--服务器申请共单

服务器申请公单,遇到三个困难点 第一个是前端页面的新建公单,服务器申请,不一定只申请一台,有可能是两台,三台,每台服务器的配置要求也有可能是不一样的,所以需要动态的添加服务器公单,如图,我实现的是点击按钮Add row 会增加一行新的服务器表格,点击Remove就删除一行服务器表格 前端html:                 <thead>                         <tr>                       <th>设备类型<

谷歌markercluster

我们有时候需要观察地图 不同地方数据的所在范围和分布密集情况,热力图和聚合点的使用无疑是最好的选择. 1.首先说说百度地图,只做国内的地图可以使用百度地图的海量点和热力图还是蛮好用的. a.海量点的最大好处是加载很多数据点的情况下不卡顿. b.如果是画mark在地图上,当点多的时候,会很卡. c.我之所以用谷歌地图的热力图,是因为我发现百度地图的热力图没有叠加功能,满足不了我的需求.比如说我想了解全国姓张,李,王的人的对比分布,我想要姓张的区域显示红色,姓李的显示蓝色,姓王的显示橙色.这个需求百

封装扩展Kendo UI Grid

封装后的代码如下: $(function () { function KendoGrid() { this.gridOptions = { height: "100%", sortable: true, reorderable: true, scrollable: true, filterable: { mode: "menu", extra: false, operators: { string: { contains: "Contains",

JavaScript事件代理和事件委托

一.概述: 那什么叫事件委托呢?它还有一个名字叫事件代理,JavaScript高级程序设计上讲:事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件.那这是什么意思呢?网上的各位大牛们讲事件委托基本上都用了同一个例子,就是取快递来解释这个现象,我仔细揣摩了一下,这个例子还真是恰当,我就不去想别的例子来解释了,借花献佛,我摘过来,大家认真领会一下事件委托到底是一个什么原理: 有三个同事预计会在周一收到快递.为签收快递,有两种办法:一是三个人在公司门口等快递:二是委托给前台

HTML5语义化标签总结

1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> <aside></aside> <article></article> <footer></footer> 注意:IE8以后不兼容H5标签,如果需要兼容IE8一下的浏览器,则需要如下操作: 如果在sublime,或者WebStrom使用 可是使

操作DOM树

操作dom树 ** appendChild方法 - 添加子节点到末尾 - 特点:类似于剪切黏贴的效果 <body> <div id="div1"> <ul id="ulid11"> <li>tom</li> <li>mary</li> <li>jack</li> </ul> </div> <div id="div2&qu

【原创】js实现一个可随意拖拽排序的菜单导航栏

1.想做这个效果的原因主要是用在UC上看新闻发现他们的导航菜单很有趣.无聊的时候在哪划着玩了很久.所以就干脆自己写一个.原效果如下 2.整体效果如下,在已推荐和未添加里面每个小方块可以触摸移动位置互换.未添加和已添加里面的小方块位置可以拖放. 3.结构分析.整体结构用红线框标出.可以分为三个部分.关注底部拖拽部分.一个div里面嵌套两个div.定位关系如图.本次教程之作一个拖拽框 1.好了,基本情况已经分析清楚先实现页面样式. <div class="wrap"> <