monolithic kernel

Mobile Safariではclickよりtouchstartが速い

November 15, 2010

    と聞いたので、ブラウザによって切り替えるようにしてみました。

    iPhone/iPod/iPadではtouchstartイベント、それ以外ではclickイベントに割り当てます。

    (function($) {$.support.mobileSafari = navigator.userAgent.match(/iPhone|iPod|iPad/) !== null;$.fn.clickortouch = function() {  if($.support.mobileSafari) {    $(this).bind.apply(this, [ 'touchstart' ].concat(Array.prototype.slice.call(arguments)));  }  else {    $(this).click.apply(this, arguments);  }  return this;};})(jQuery);

    使うときはこんな感じで。

    $('a').clickortouch(function() {  alert('Hello, World!');  return false;});

    試してみたところ、clickイベントを使ったときのようなワンテンポ遅れる感じがなくなってかなり快適になりました。

    使いどころはあまり無い気がしますけどね。