使用手势库Touch.js

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;
});

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值