4.2Zepto之移动端Touch事件

移动端Touch事件

1.Zepto实现tap事件

虽然tap事件是Zepto自己封装的事件, 但是无论如何封装肯定都是通过原生JS来实现的
在原生的JS中专门为移动端新增了如下几个事件:
touchstart: 手指按下
touchmove: 手指在元素上移动
touchend : 手指抬起
注意点: 这几个事件只支持移动端, 不支持PC端

2.Touch事件对象

移动端的touch事件也是一个事件, 所以被触发的时候系统也会自动传递一个事件对象给我们
移动端touch事件对象中比较重要的三个子对象:

touches: 当前屏幕上所有手指的列表
targetTouches: 保存了元素上所有的手指列表
changedTouches: 当前屏幕上刚刚接触的手指或者离开的手指

let oDiv1 = document.querySelector(".box1");
    oDiv1.ontouchstart = function (event) {
       console.log("touches1", event.touches);
       console.log("targetTouches1", event.targetTouches);
    }
    oDiv1.ontouchend = function (event) {
        console.log("touches2", event.touches);
        console.log("targetTouches2", event.targetTouches);
    }
  1. touches和targetTouches
    如果都是将手指按到了同一个元素上, 那么这两个对象中保存的内容是一样的
    如果是将手指按到了不同的元素上, 那么这个两个对象中保存的内容不一样
    touches保存的是所有元素中的手指, 而targetTouches保存的是当前元素中的手指
  2. changedTouches
    在ontouchstart中保存的是刚刚新增的手指
    在ontouchend中保存的是刚刚离开的手指
3.Touch事件位置
  1. 1.screenX/screenY是相对于屏幕左上角的偏移位
  2. clientX/clientY是相对于可视区域左上角的偏移位
  3. pageX/pageY是相对于内容左上角的偏移位
<div></div>
<script>
    let oDiv = document.querySelector("div");
    oDiv.ontouchstart = function (event) {
        console.log(event.targetTouches[0]);
        console.log(event.targetTouches[0].screenX);
        console.log(event.targetTouches[0].screenY);
        console.log(event.targetTouches[0].clientX); 
        console.log(event.targetTouches[0].clientY); 
        console.log(event.targetTouches[0].pageX);  
        console.log(event.targetTouches[0].pageY);  
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值