js实现简易的touch事件(es5)

    var btn = document.querySelector('#btn');
    btn.ontouchstart = function (e) {
        var point = e.changedTouches[0];
        this.strX = point.pageX;
        this.strY = point.pageY;
        this.isMove = false;
    }
    btn.ontouchmove = function (e) {
        var point = e.changedTouches[0];
        //手指按下去或多或少都会有滑动。一般以10像素为偏差值
        var chengeX = point.pageX - this.strX,
            chengeY = point.pageY - this.strY;
        this.chengeX = chengeX;
        this.chengeY = chengeY;
        if(Math.abs(chengeX) > 10 || Math.abs(chengeY) > 10){
            this.isMove = true;
        }
    }
    btn.ontouchend = function (e) {
        var point = e.changedTouches[0];
        if(!this.isMove) {
            console.log('我是点击操作--');
            return;
        }
        var dir = null;
        if(Math.abs(this.chengeX)> Math.abs(this.chengeY)) {
            dir = this.chengeX < 0 ? 'left' : 'right';
        }else {
            dir = this.chengeY < 0 ? 'up' : 'down';
        }
        console.log(dir);
    }
复制代码

转载于:https://juejin.im/post/5c791c4f6fb9a049b07e0f63

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值