移動端touch模組

來源:互聯網
上載者:User

標籤:

在自己寫touch事件時,總是會出現各種各樣的bug,正好發現zepto的touch模組,很好用,而且由於zepto和jquery文法的相似性,這個模組也可以直接引用到jquery中,

得花時間好好消化一番,這樣以後自己就可以隨心所欲了。。。

var touch = {},touchTimeout, tapTimeout, swipeTimeout, longTapTimeout,longTapDelay = 750,gesturefunction swipeDirection(x1, x2, y1, y2) { //判定滑動方向return Math.abs(x1 - x2) >=Math.abs(y1 - y2) ? (x1 - x2 > 0 ? ‘Left‘ : ‘Right‘) : (y1 - y2 > 0 ? ‘Up‘ : ‘Down‘)}function longTap() { //長按longTapTimeout = nullif (touch.last) {touch.el.trigger(‘longTap‘)touch = {} }}function cancelLongTap() { //取消長按if (longTapTimeout) clearTimeout(longTapTimeout)longTapTimeout = null}function cancelAll() { //取消全部if (touchTimeout) clearTimeout(touchTimeout)if (tapTimeout) clearTimeout(tapTimeout)if (swipeTimeout) clearTimeout(swipeTimeout)if (longTapTimeout) clearTimeout(longTapTimeout)touchTimeout = tapTimeout = swipeTimeout = longTapTimeout = nulltouch = {}}function isPrimaryTouch(event){return (event.pointerType == ‘touch‘ ||event.pointerType == event.MSPOINTER_TYPE_TOUCH)&& event.isPrimary}function isPointerEventType(e, type){return (e.type == ‘pointer‘+type ||e.type.toLowerCase() == ‘mspointer‘+type)}$(document).ready(function(){var now, delta, deltaX = 0, deltaY = 0, firstTouch, _isPointerTypeif (‘MSGesture‘ in window) {//IE10中新加入的對進階使用者輸入的識別支援gesture = new MSGesture() //執行個體手勢對象gesture.target = document.body}$(document).bind(‘MSGestureEnd‘, function(e){ //類似mouseup,相容觸屏和滑鼠操作var swipeDirectionFromVelocity =e.velocityX > 1 ? ‘Right‘ : e.velocityX < -1 ? ‘Left‘ : e.velocityY > 1 ? ‘Down‘ : e.velocityY < -1 ? ‘Up‘ : null;if (swipeDirectionFromVelocity) {touch.el.trigger(‘swipe‘)touch.el.trigger(‘swipe‘+ swipeDirectionFromVelocity)}}).on(‘touchstart MSPointerDown pointerdown‘, function(e){if((_isPointerType = isPointerEventType(e, ‘down‘)) &&!isPrimaryTouch(e)) returnfirstTouch = _isPointerType ? e : e.touches[0]if (e.touches && e.touches.length === 1 && touch.x2) {// Clear out touch movement data if we have it sticking around// This can occur if touchcancel doesn‘t fire due to preventDefault, etc.touch.x2 = undefinedtouch.y2 = undefined}now = Date.now()delta = now - (touch.last || now)touch.el = $(‘tagName‘ in firstTouch.target ?firstTouch.target : firstTouch.target.parentNode)touchTimeout && clearTimeout(touchTimeout)touch.x1 = firstTouch.pageXtouch.y1 = firstTouch.pageYif (delta > 0 && delta <= 250) touch.isDoubleTap = truetouch.last = nowlongTapTimeout = setTimeout(longTap, longTapDelay)// adds the current touch contact for IE gesture recognitionif (gesture && _isPointerType) gesture.addPointer(e.pointerId);}).on(‘touchmove MSPointerMove pointermove‘, function(e){if((_isPointerType = isPointerEventType(e, ‘move‘)) &&!isPrimaryTouch(e)) returnfirstTouch = _isPointerType ? e : e.touches[0]cancelLongTap()touch.x2 = firstTouch.pageXtouch.y2 = firstTouch.pageYdeltaX += Math.abs(touch.x1 - touch.x2)deltaY += Math.abs(touch.y1 - touch.y2)}).on(‘touchend MSPointerUp pointerup‘, function(e){if((_isPointerType = isPointerEventType(e, ‘up‘)) &&!isPrimaryTouch(e)) returncancelLongTap()// swipeif ((touch.x2 && Math.abs(touch.x1 - touch.x2) > 30) ||(touch.y2 && Math.abs(touch.y1 - touch.y2) > 30))swipeTimeout = setTimeout(function() {touch.el.trigger(‘swipe‘)touch.el.trigger(‘swipe‘ + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)))touch = {}}, 0)// normal tapelse if (‘last‘ in touch)// don‘t fire tap when delta position changed by more than 30 pixels,// for instance when moving to a point and back to originif (deltaX < 30 && deltaY < 30) {// delay by one tick so we can cancel the ‘tap‘ event if ‘scroll‘ fires// (‘tap‘ fires before ‘scroll‘)tapTimeout = setTimeout(function() {// trigger universal ‘tap‘ with the option to cancelTouch()// (cancelTouch cancels processing of single vs double taps for faster ‘tap‘ response)var event = $.Event(‘tap‘)event.cancelTouch = cancelAlltouch.el.trigger(event)// trigger double tap immediatelyif (touch.isDoubleTap) {if (touch.el) touch.el.trigger(‘doubleTap‘)touch = {}}// trigger single tap after 250ms of inactivityelse {touchTimeout = setTimeout(function(){touchTimeout = nullif (touch.el) touch.el.trigger(‘singleTap‘)touch = {}}, 250)}}, 0)} else {touch = {}}deltaX = deltaY = 0})// when the browser window loses focus,// for example when a modal dialog is shown,// cancel all ongoing events.on(‘touchcancel MSPointerCancel pointercancel‘, cancelAll)// scrolling the window indicates intention of the user// to scroll, not tap or swipe, so cancel all ongoing events$(window).on(‘scroll‘, cancelAll)});[‘swipe‘, ‘swipeLeft‘, ‘swipeRight‘, ‘swipeUp‘, ‘swipeDown‘,‘doubleTap‘, ‘tap‘, ‘singleTap‘, ‘longTap‘].forEach(function(eventName){$.fn[eventName] = function(callback){ return this.on(eventName, callback) }})})(Zepto)

 

移動端touch模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.