首頁 >web前端 >js教程 >hammer.js實現行動端的圖片手勢放大功能

hammer.js實現行動端的圖片手勢放大功能

php中世界最好的语言
php中世界最好的语言原創
2018-05-11 14:41:514075瀏覽

 var reqAnimationFrame = (function() {
       return window[Hammer.prefixed(window, 'requestAnimationFrame')] || function(callback) {
         window.setTimeout(callback, 1000 / 60);
       };
     })();
     var el = $('img');
     var ticking = false;
     var transform;
     var initScale = 1;
     var _eImg = '';
     for (var m = 0; m < el.length; m++) {
       var mc = new Hammer.Manager(el[m]);
       mc.add(new Hammer.Pan({
         threshold: 0,
         pointers: 0
       }));
       mc.add(new Hammer.Swipe()).recognizeWith(mc.get(&#39;pan&#39;));
       mc.add(new Hammer.Pinch({
         threshold: 0
       })).recognizeWith(mc.get(&#39;pan&#39;));
       mc.on(&#39;panstart panmove&#39;, onPan);
       mc.on(&#39;pinchstart pinchmove&#39;, onPinch);
       mc.on(&#39;swipe&#39;, onSwipe);
     }
 
     function resetElement() {
       el.addClass(&#39;animate&#39;);
       transform = {
         translate: {
           x: 0,
           y: 0
         },
         scale: 1,
         angle: 0,
         rx: 0,
         ry: 0,
         rz: 0
       };
       requestElementUpdate();
     }
 
     function updateElementTransform() {
       var value = [&#39;translate3d(&#39; + transform.translate.x + &#39;px, &#39; + transform.translate.y + &#39;px, 0)&#39;, &#39;scale(&#39; + transform.scale + &#39;, &#39; + transform.scale + &#39;)&#39;, &#39;rotate3d(&#39; + transform.rx + &#39;,&#39; + transform.ry + &#39;,&#39; + transform.rz + &#39;,&#39; + transform.angle + &#39;deg)&#39;];
       value = value.join(&#39; &#39;);
       if (_eImg != &#39;&#39;) {
         _eImg.style.webkitTransform = value;
         _eImg.style.mozTransform = value;
         _eImg.style.transform = value;
         //_eImg.css({ &#39;transform&#39;: value }, { &#39;-webkit-transform&#39;: value });
       }
       ticking = false;
     }
 
     function requestElementUpdate() {
       if (!ticking) {
         reqAnimationFrame(updateElementTransform);
         ticking = true;
       }
     }
 
     function onPan(ev) {
       el.removeClass(&#39;animate&#39;);
       transform.translate = {
         x: ev.deltaX,
         y: ev.deltaY
       };
     }
 
     function onPinch(ev) {
       if (ev.type == &#39;pinchstart&#39;) {
         initScale = transform.scale || 1;
       }
       el.removeClass(&#39;animate&#39;);
       transform.scale = initScale * ev.scale;
       requestElementUpdate();
       _eImg = ev.target;
       return _eImg;
     }
 
     function onSwipe(ev) {
       var angle = 10;
       transform.ry = (ev.direction & Hammer.DIRECTION_HORIZONTAL) ? 1 : 0;
       transform.rx = (ev.direction & Hammer.DIRECTION_VERTICAL) ? 1 : 0;
       transform.angle = (ev.direction & (Hammer.DIRECTION_RIGHT | Hammer.DIRECTION_UP)) ? angle : -angle;
       requestElementUpdate();
       _eImg = ev.target;
       return _eImg;
     }
     resetElement();

我相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

Node.js檔案系統動作

#es6解構有哪些方法

以上是hammer.js實現行動端的圖片手勢放大功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn