移动设备手势事件库Touch.js使用详解
程序员文章站
2022-11-26 11:16:01
touch.js手势库是专门在webkit内核浏览器的移动设备中使用中设计的, touch.js是移动设备上的手势识别与事件库。touch.js基于原生事件,支持事件代理,...
touch.js手势库是专门在webkit内核浏览器的移动设备中使用中设计的, touch.js是移动设备上的手势识别与事件库。touch.js基于原生事件,支持事件代理, 性能更好,极简的api,秒速上手等优势。
1、旋转事件- startrotate
var angle = 0; touch.on('#target', 'touchstart', function(ev){ ev.startrotate(); ev.preventdefault(); }); touch.on('#target', 'rotate', function(ev){ var totalangle = angle + ev.rotation; if(ev.fingerstatus === 'end'){ angle = angle + ev.rotation; } this.style.webkittransform = 'rotate(' + totalangle + 'deg)'; });
2、双指缩放事件-scale
var target = document.getelementbyid("target"); target.style.webkittransition = 'all ease 0.05s'; touch.on('#target', 'touchstart', function(ev){ ev.preventdefault(); }); var initialscale = 1; var currentscale; touch.on('#target', 'pinchend', function(ev){ currentscale = ev.scale - 1; currentscale = initialscale + currentscale; currentscale = currentscale > 2 ? 2 : currentscale; currentscale = currentscale < 1 ? 1 : currentscale; this.style.webkittransform = 'scale(' + currentscale + ')'; log("当前缩放比例为:" + currentscale + "."); }); touch.on('#target', 'pinchend', function(ev){ initialscale = currentscale; });
3、识别单击, 双击和长按事件-tap & hold
touch.on('#target', 'hold tap doubletap', function(ev){ //console.log(ev.type); });
4、向左, 向右滑动-swipe
touch.on('#target', 'touchstart', function(ev){ ev.preventdefault(); }); var target = document.getelementbyid("target"); target.style.webkittransition = 'all ease 0.2s'; touch.on(target, 'swiperight', function(ev){ this.style.webkittransform = "translate3d(" + rt + "px,0,0)"; log("向右滑动."); }); touch.on(target, 'swipeleft', function(ev){ log("向左滑动."); this.style.webkittransform = "translate3d(-" + this.offsetleft + "px,0,0)"; });
5、拖拽事件-drag
touch.on('#target', 'touchstart', function(ev){ ev.preventdefault(); }); var target = document.getelementbyid("target"); var dx, dy; touch.on('#target', 'drag', function(ev){ dx = dx || 0; dy = dy || 0; log("当前x值为:" + dx + ", 当前y值为:" + dy +"."); var offx = dx + ev.x + "px"; var offy = dy + ev.y + "px"; this.style.webkittransform = "translate3d(" + offx + "," + offy + ",0)"; }); touch.on('#target', 'dragend', function(ev){ dx += ev.x; dy += ev.y; });
6、原生事件-touch
touch.on('#target', 'touchstart touchmove touchend', function(ev){ console.log(ev.type); });
touch.js官方网站:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。