이번에는 Zepto 탭 이벤트를 사용하여 관통하고 탭하는 방법을 보여 드리겠습니다(코드 포함). Zepto 탭 이벤트를 사용하여 관통하고 탭할 때 주의 사항은 무엇입니까? 봐.
먼저 젭토 탭이벤트 침투란?탭 이벤트 침투는 여러 수준에 바인딩된 이벤트가 있음을 의미하며, 상위 수준은 탭 이벤트에 바인딩되고, 하위 수준은 클릭 이벤트에 바인딩됩니다. 트리거되고 이벤트가 발생합니다. 하위 레이어가 이유:
zepto는 탭 이벤트가 문서에 버블링될 때 트리거되도록 구현했기 때문입니다. 즉, 탭 이벤트가 문서에 바인딩되어 있고 클릭 이벤트가 실행을 지연시켰기 때문입니다.
아래에는 zepto.1.1.6 탭 이벤트의 소스 코드가 게시되어 있습니다.
<span style="font-size: 14px;">;(function($){<br> var touch = {},<br> touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,<br> longTapDelay = 750,<br> gesture<br> function swipeDirection(x1, x2, y1, y2) {<br> return Math.abs(x1 - x2) >=<br> Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')<br> }<br> function longTap() {<br> longTapTimeout = null<br> if (touch.last) {<br> touch.el.trigger('longTap')<br> touch = {}<br> }<br> }<br> function cancelLongTap() {<br> if (longTapTimeout) clearTimeout(longTapTimeout)<br> longTapTimeout = null<br> }<br> function cancelAll() {<br> if (touchTimeout) clearTimeout(touchTimeout)<br> if (tapTimeout) clearTimeout(tapTimeout)<br> if (swipeTimeout) clearTimeout(swipeTimeout)<br> if (longTapTimeout) clearTimeout(longTapTimeout)<br> touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = null<br> touch = {}<br> }<br> function isPrimaryTouch(event){<br> return (event.pointerType == 'touch' ||<br> event.pointerType == event.MSPOINTER_TYPE_TOUCH)<br> && event.isPrimary<br> }<br> function isPointerEventType(e, type){<br> return (e.type == 'pointer'+type ||<br> e.type.toLowerCase() == 'mspointer'+type)<br> }<br> $(document).ready(function(){<br> var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerType<br> if ('MSGesture' in window) {<br> gesture = new MSGesture()<br> gesture.target = document.body<br> }<br> $(document)<br> .bind('MSGestureEnd', function(e){<br> var swipeDirectionFromVelocity =<br> e.velocityX > 1 ? 'Right' : e.velocityX 1 ? 'Down' : e.velocityY if (swipeDirectionFromVelocity) {<br> touch.el.trigger('swipe')<br> touch.el.trigger('swipe'+ swipeDirectionFromVelocity)<br> }<br> })<br> .on('touchstart MSPointerDown pointerdown', function(e){<br> if((_isPointerType = isPointerEventType(e, 'down')) &&<br> !isPrimaryTouch(e)) return<br> firstTouch = _isPointerType ? e : e.touches[0]<br> if (e.touches && e.touches.length === 1 && touch.x2) {<br> // Clear out touch movement data if we have it sticking around<br> // This can occur if touchcancel doesn't fire due to preventDefault, etc.<br> touch.x2 = undefined<br> touch.y2 = undefined<br> }<br> now = Date.now()<br> delta = now - (touch.last || now)<br> touch.el = $('tagName' in firstTouch.target ?<br> firstTouch.target : firstTouch.target.parentNode)<br> touchTimeout && clearTimeout(touchTimeout)<br> touch.x1 = firstTouch.pageX<br> touch.y1 = firstTouch.pageY<br> if (delta > 0 && delta touch.last = now<br> longTapTimeout = setTimeout(longTap, longTapDelay)<br> // adds the current touch contact for IE gesture recognition<br> if (gesture && _isPointerType) gesture.addPointer(e.pointerId);<br> })<br> .on('touchmove MSPointerMove pointermove', function(e){<br> if((_isPointerType = isPointerEventType(e, 'move')) &&<br> !isPrimaryTouch(e)) return<br> firstTouch = _isPointerType ? e : e.touches[0]<br> cancelLongTap()<br> touch.x2 = firstTouch.pageX<br> touch.y2 = firstTouch.pageY<br> deltaX += Math.abs(touch.x1 - touch.x2)<br> deltaY += Math.abs(touch.y1 - touch.y2)<br> })<br> .on('touchend MSPointerUp pointerup', function(e){<br> if((_isPointerType = isPointerEventType(e, 'up')) &&<br> !isPrimaryTouch(e)) return<br> cancelLongTap()<br> // swipe<br> if ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||<br> (touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))<br> swipeTimeout = setTimeout(function() {<br> touch.el.trigger('swipe')<br> touch.el.trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))<br> touch = {}<br> }, 0)<br> // normal tap<br> else if ('last' in touch)<br> // don't fire tap when delta position changed by more than 30 pixels,<br> // for instance when moving to a point and back to origin<br> if (deltaX // delay by one tick so we can cancel the 'tap' event if 'scroll' fires<br> // ('tap' fires before 'scroll')<br> tapTimeout = setTimeout(function() {<br> // trigger universal 'tap' with the option to cancelTouch()<br> // (cancelTouch cancels processing of single vs double taps for faster 'tap' response)<br> var event = $.Event('tap')<br> event.cancelTouch = cancelAll<br> touch.el.trigger(event)<br> // trigger double tap immediately<br> if (touch.isDoubleTap) {<br> if (touch.el) touch.el.trigger('doubleTap')<br> touch = {}<br> }<br> // trigger single tap after 250ms of inactivity<br> else {<br> touchTimeout = setTimeout(function(){<br> touchTimeout = null<br> if (touch.el) touch.el.trigger('singleTap')<br> touch = {}<br> }, 250)<br> }<br> }, 0)<br> } else {<br> touch = {}<br> }<br> deltaX = deltaY = 0<br> })<br> // when the browser window loses focus,<br> // for example when a modal dialog is shown,<br> // cancel all ongoing events<br> .on('touchcancel MSPointerCancel pointercancel', cancelAll)<br> // scrolling the window indicates intention of the user<br> // to scroll, not tap or swipe, so cancel all ongoing events<br> $(window).on('scroll', cancelAll)<br> })<br> ;['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown',<br> 'doubleTap', 'tap', 'singleTap', 'longTap'].forEach(function(eventName){<br> $.fn[eventName] = function(callback){ return this.on(eventName, callback) }<br> })<br>})(Zepto)</span>
zepto 소스 코드에 따르면 탭 이벤트가 터치 이벤트를 통해 문서를 시뮬레이션합니다. 따라서 사용자가 탭 이벤트(touchstart, touchend)를 클릭하면 트리거되기 전에 문서까지 버블링되어야 합니다. 단, 사용자는 터치시작 및 터치엔드 시 클릭 이벤트를 발생시키게 되는데 이때 클릭 이벤트는 300ms 동안 지연되며, 이 300ms 이내에 탭 이벤트가 완료되면 상위 요소가 삭제되거나 숨겨집니다. 300ms가 도달하면 클릭 이벤트의 원리에 따라(클릭 이벤트의 요소가 최상위에 있을 때 클릭 이벤트에 들어가게 되므로 z-index 설정을 잘못하면 클릭 이벤트가 발생하지 않는 경우도 있음), 하단 이벤트가 실행되어 관통 현상이 나타납니다. 하위 레이어를 입력 요소로 설정합니다. 클릭 이벤트가 바인딩되지 않더라도 기본 포커스가 팝업 키보드에 있기 때문에 침투 현상이 특히 심각합니다.
해결책:
1. 클릭 이벤트 실행 지연을 방지하기 위해 github에 fastclick 플러그인이 있습니다. 파일을 import한 후, 다음 코드를 추가하고, 침투를 유발할 수 있는 탭 이벤트 요소를 클릭으로 교체합니다.
$(function(){ new FastClick(document.body); })
$("#close").on("touchend",function(e){
$("#alertBox").hide();
e.preventDefault();
});
4. 하위 클릭 이벤트가 발생하지 않도록 상위 요소의 사라짐을 지연해 보세요. (iOS9.2의 WeChat 6.3.15에서 테스트했습니다.) 그러나 이는 약간 나쁜 경험입니다. CSS3 전환을 사용하여 경험을 개선할 수 있습니다.
setTimeout(function(){ $(#alertBox).hide(); } , 350 );
5. 모든 탭을 클릭으로 대체하세요. 경험 문제를 일으키는 클릭 지연으로 인해 fastclick 플러그인을 추가하는 것이 가장 좋습니다.
다음은 제가 작성한 간단한 예입니다. 휴대폰을 사용하여 http://property.pingan.com/app/test/jltest/tap-through.html?a=1
에 액세스할 수 있습니다. 예제를 통해 기본 버튼이 이벤트 침투 후 누르는 효과가 있음을 명확하게 볼 수 있습니다. 자주 테스트하는 동안 WeChat은 페이지를 캐시하고 즉시 수정된 콘텐츠를 볼 수 없기 때문에 a=1과 같은 쓸모없는 매개변수를 URL에 추가하여 브라우저가 다시 로드되도록 할 수 있습니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<title>test-tap-through</title>
<script src="js/zepto.min.js" charset="utf-8"></script>
<style media="screen">
body{
margin: 0;
padding: 0;
}
.test1,.test2{
position: relative;
}
.button{
width: 90%;
height: 75px;
background-color: #00ffff;
margin: 5%;
line-height: 75px;
text-align: center;
font-size: 40px;
}
.box{
position: absolute;
top:0;
left: 0;
width: 50%;
height: 200px;
background-color: #ff00ff;
margin: 5%;
line-height: 100px;
text-align: center;
font-size: 40px;
z-index: 100;
}
</style>
</head>
<body>
<p>
<input type="button" id="button1" value="button1">
<input type="button" id="button2" value="button2">
<p id="box1" style="display:none">box1</p>
<p id="box2" style="display:none">box2</p>
</p>
<p>
<input type="button" id="button3" value="button3">
<input type="button" id="button4" value="button4">
<p id="box3" style="display:none">box3</p>
<p id="box4" style="display:none">box4</p>
</p>
</body>
<script type="text/javascript">
$("#button1").click(function(){
$("#box2").hide();
$("#box1").show();
});
$("#button2").click(function(){
$("#box1").hide();
$("#box2").show();
});
$("#box2").tap(function(){
$("#box2").hide();
});
$("#box1").tap(function(){
$("#box1").hide();
});
$("#button3").click(function(){
$("#box4").hide();
$("#box3").show();
});
$("#button4").click(function(){
$("#box3").hide();
$("#box4").show();
});
$("#box3").tap(function(){
setTimeout(function(){$("#box3").hide();},350);
});
$("#box4").tap(function(){
setTimeout(function(){$("#box4").hide();},350);
});
</script>
</html>
이 기사의 사례를 읽으신 후 방법을 마스터하셨다고 생각합니다. 더 흥미로운 정보를 보려면 PHP 중국어 웹사이트의 다른 관련 기사를 주목하세요!
추천 자료:
JS 객체 속성 및 메서드에 액세스하는 방법Yuanshengcss3을 사용하여 링 로딩 진행률 표시줄을 구현하는 방법위 내용은 Zepto 탭 이벤트 침투 및 포인트 침투 사용법(코드 포함)의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

JavaScript는 1995 년에 시작하여 Brandon Ike에 의해 만들어졌으며 언어를 C로 실현했습니다. 1.C Language는 JavaScript의 고성능 및 시스템 수준 프로그래밍 기능을 제공합니다. 2. JavaScript의 메모리 관리 및 성능 최적화는 C 언어에 의존합니다. 3. C 언어의 크로스 플랫폼 기능은 자바 스크립트가 다른 운영 체제에서 효율적으로 실행하는 데 도움이됩니다.

JavaScript는 브라우저 및 Node.js 환경에서 실행되며 JavaScript 엔진을 사용하여 코드를 구문 분석하고 실행합니다. 1) 구문 분석 단계에서 초록 구문 트리 (AST)를 생성합니다. 2) 컴파일 단계에서 AST를 바이트 코드 또는 기계 코드로 변환합니다. 3) 실행 단계에서 컴파일 된 코드를 실행하십시오.

Python 및 JavaScript의 미래 추세에는 다음이 포함됩니다. 1. Python은 과학 컴퓨팅 분야에서의 위치를 통합하고 AI, 2. JavaScript는 웹 기술의 개발을 촉진하고, 3. 교차 플랫폼 개발이 핫한 주제가되고 4. 성능 최적화가 중점을 둘 것입니다. 둘 다 해당 분야에서 응용 프로그램 시나리오를 계속 확장하고 성능이 더 많은 혁신을 일으킬 것입니다.

개발 환경에서 Python과 JavaScript의 선택이 모두 중요합니다. 1) Python의 개발 환경에는 Pycharm, Jupyternotebook 및 Anaconda가 포함되어 있으며 데이터 과학 및 빠른 프로토 타이핑에 적합합니다. 2) JavaScript의 개발 환경에는 Node.js, VScode 및 Webpack이 포함되어 있으며 프론트 엔드 및 백엔드 개발에 적합합니다. 프로젝트 요구에 따라 올바른 도구를 선택하면 개발 효율성과 프로젝트 성공률이 향상 될 수 있습니다.

예, JavaScript의 엔진 코어는 C로 작성되었습니다. 1) C 언어는 효율적인 성능과 기본 제어를 제공하며, 이는 JavaScript 엔진 개발에 적합합니다. 2) V8 엔진을 예를 들어, 핵심은 C로 작성되며 C의 효율성 및 객체 지향적 특성을 결합하여 C로 작성됩니다.

JavaScript는 웹 페이지의 상호 작용과 역학을 향상시키기 때문에 현대 웹 사이트의 핵심입니다. 1) 페이지를 새로 고치지 않고 콘텐츠를 변경할 수 있습니다. 2) Domapi를 통해 웹 페이지 조작, 3) 애니메이션 및 드래그 앤 드롭과 같은 복잡한 대화식 효과를 지원합니다. 4) 성능 및 모범 사례를 최적화하여 사용자 경험을 향상시킵니다.

C 및 JavaScript는 WebAssembly를 통한 상호 운용성을 달성합니다. 1) C 코드는 WebAssembly 모듈로 컴파일되어 컴퓨팅 전력을 향상시키기 위해 JavaScript 환경에 도입됩니다. 2) 게임 개발에서 C는 물리 엔진 및 그래픽 렌더링을 처리하며 JavaScript는 게임 로직 및 사용자 인터페이스를 담당합니다.

JavaScript는 웹 사이트, 모바일 응용 프로그램, 데스크탑 응용 프로그램 및 서버 측 프로그래밍에서 널리 사용됩니다. 1) 웹 사이트 개발에서 JavaScript는 HTML 및 CSS와 함께 DOM을 운영하여 동적 효과를 달성하고 jQuery 및 React와 같은 프레임 워크를 지원합니다. 2) 반응 및 이온 성을 통해 JavaScript는 크로스 플랫폼 모바일 애플리케이션을 개발하는 데 사용됩니다. 3) 전자 프레임 워크를 사용하면 JavaScript가 데스크탑 애플리케이션을 구축 할 수 있습니다. 4) node.js는 JavaScript가 서버 측에서 실행되도록하고 동시 요청이 높은 높은 요청을 지원합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

VSCode Windows 64비트 다운로드
Microsoft에서 출시한 강력한 무료 IDE 편집기

안전한 시험 브라우저
안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구
