2012-01-30 3 views
9

모바일 장치 (iPad, Galaxy Tab)에서 웹 사이트를 볼 때 요소 (일반 링크 또는 javascript/jquery를 사용하여 클릭 할 수있는 다른 요소)를 클릭하면 항상 지연이 있습니다.모바일 터치 장치에서 클릭 지연을 제거하는 방법이 있습니까?

온라인으로 읽는 동안 브라우저에서 터치 시작과 함께 touchend 이벤트가 발생하고 이후에 일반 클릭 이벤트가 트리거된다는 것을 알았습니다. 지연된 클릭 이벤트를 제거하고 반응 형 탭을 사용하는 방법이 있습니까? 어쩌면 자바 스크립트, 또는 다른 것을 사용하여?

답변

2

웹 페이지를 직접 작성하는 경우 touchstart 및 touchend에 대한 수신기를 등록하고 지연없이 터치 엔드에서 직접 onclick 코드를 트리거 할 수 있습니다. 당신이 "빠른 버튼"을 만들기 위해 구글에서이 설명에서 봐 요소

에 (일부 지연과) 파견하는 클릭 이벤트를 브라우저를 이동 터치의 이벤트를 처리하지 않으시면

: http://code.google.com/intl/de-DE/mobile/articles/fast_buttons.html

+0

어떻게 할 것인지에 대한 코드를 게시 할 수 있다면 감사하겠습니다. 일반적으로 웹 사이트를 만들 때 jQuery를 사용하고 .trigger()를 사용하여 click 이벤트를 트리거 할 수 있음을 알고 있습니다. 하지만 내가 수동으로 필요할 때마다 수동으로 추가하지 않고 각 "탭"에서 "클릭"이라고 자동으로 부를 수있는 방법을 모르겠다. – Gabriel

+1

나는 적절한 링크를 사용하여 답변을 업데이트했습니다. –

5

Google의 라이브러리 (https://stackoverflow.com/a/9370637/1491212) 자체에서 변형 된 jQuery 플러그인을 작성했습니다. 이 같은

사용 : $('mySelector').fastClick(handler);

(function($){ 
    var clickbuster = { 
     preventGhostClick: function(x, y) { 
      clickbuster.coordinates.push(x, y); 
      window.setTimeout(clickbuster.pop, 2500); 
     }, 
     pop: function() { 
      clickbuster.coordinates.splice(0, 2); 
     }, 
     onClick: function(event) { 
      for (var i = 0; i < clickbuster.coordinates.length; i += 2) { 
      var x = clickbuster.coordinates[i]; 
      var y = clickbuster.coordinates[i + 1]; 
      if (Math.abs(event.clientX - x) < 25 && Math.abs(event.clientY - y) < 25) { 
       event.stopPropagation(); 
       event.preventDefault(); 
      } 
      } 
     } 
    }; 


    var methods = { 
     init: function(handler){ 
      return this.each(function() { 
       var $this = $(this), 
        data = $this.data('fastClick'); 
       if(!data){ 
        this.addEventListener('touchstart', methods.handleEvent, false); 
        this.addEventListener('click', methods.handleEvent, false); 
        $this.data('fastClick', { 
         target: $this, 
         handler: handler 
        }); 
       } 
      }); 
     }, 
     handleEvent:function(event) { 
      switch (event.type) { 
      case 'touchstart': $(this).fastClick('onTouchStart',event); break; 
      case 'touchmove': $(this).fastClick('onTouchMove',event); break; 
      case 'touchend': $(this).fastClick('onClick',event); break; 
      case 'click': $(this).fastClick('onClick',event); break; 
      } 
     }, 
     onTouchStart: function(event) { 
      event.stopPropagation(); 
      this[0].addEventListener('touchend', methods.handleEvent, false); 
      var _this = this; 
      document.body.addEventListener('touchmove', function(event){ 
      methods.handleEvent.apply(_this,[event]); 
      }, false); 

      $(this).data('fastClick').startX = event.touches[0].clientX; 
      $(this).data('fastClick').startY = event.touches[0].clientY; 
     }, 
     onTouchMove: function(event) { 
      if (Math.abs(event.touches[0].clientX - this.data('fastClick').startX) > 10 || 
       Math.abs(event.touches[0].clientY - this.data('fastClick').startY) > 10) { 
       this.fastClick('reset'); 
      } 
     }, 
     onClick: function(event) { 
      event.stopPropagation(); 
      $(this).fastClick('reset'); 
      $(this).data('fastClick').handler.call(this,event); 

      if (event.type == 'touchend') { 
      clickbuster.preventGhostClick($(this).data('fastClick').startX, $(this).data('fastClick').startY); 
      } 
     }, 
     reset: function() { 
      this[0].removeEventListener('touchend', methods.handleEvent, false); 
      document.body.removeEventListener('touchmove', methods.handleEvent, false); 
     } 
    } 
    $.fn.fastClick = function(method) { 
     if (methods[method]) { 
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
     } else if (typeof method === 'object' || typeof method === 'function' || !method) { 
      return methods.init.apply(this, arguments); 
     } else { 
      $.error('Method ' + method + ' does not exist on jQuery.hScroll'); 
     } 

    } 

    clickbuster.coordinates = []; 
    document.addEventListener('click', clickbuster.onClick, true); 

})(jQuery); 
+0

이것은 멋지지만, 내가 클릭 한 것을 알 수있는 방법을 찾지 못한다 - $ (this)는 이벤트를 정상적으로 바인딩하는 것과는 달리, 클릭 한 것을 반환하지 않는다. '$ ('a'). fastClick (function() {alert ($ (this) .attr ('href'))})'예를 들어 어떻게 작동시킬 수 있습니까? –

+0

이 문제도 발생했습니다. 내 대답을 편집했습니다. onClick 핸들러는 올바른 범위에서 호출되어야합니다. handler.call (this, event); 편집보기 –

+1

더 이상 필요하지 않습니까? 진저 브레드에서 앞으로 뷰포트 메타 태그 확대/축소 기능을 사용 중지하면 지연이 발생하지 않는다고하는 몇 가지 기사를 보았습니다. –

0

나는 감지를 사용하는 경우 모더 나이저 같은 장치 지원 터치. 나는 'click' 또는 'touchend' 옵션이 터치 장치 인 경우 touchClick이라는 var에 내용을 채 웁니다. jquery에서 나는 간단하게 다음과 같이 호출한다 :

$('element').on(touchClick, function(e){ //do something }); 

매우 작은 설치 공간을 가지고있다.

+0

이 방법은 브라우저가 터치 스크린 지원 랩톱에서 사용되는 경우 위험합니다. 마우스 클릭이 작동하지 않습니다 ... – franzlorenzon

관련 문제