HTML5触摸事件

HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享。今天为大家介绍的事件主要是触摸事件:touchstart、touchmove和touchend。

touchstart事件:当手指触摸屏幕时候触发,即使已经有一个手指放在屏幕上也会触发。
touchmove事件:当手指在屏幕上滑动的时候连续地触发。在这个事件发生期间,调用preventDefault()事件可以阻止滚动。
touchend事件:当手指从屏幕上离开的时候触发。
touchcancel事件:当系统停止跟踪触摸的时候触发。关于这个事件的确切出发时间,文档中并没有具体说明,咱们只能去猜测了。


HTML5屏幕触摸的四个事件touchstart、touchmove、touchend、touchcancel
 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <title>touch 事件</title>
    <style>
        body{
            margin: 0;
            padding: 0;
        }
        .box{
            height: 200px;
            width: 200px;
            background: red;
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        /*
        * 1.touchstart    手指刚刚触发到屏幕的时候
        * 2.touchmove     手指在屏幕上滑动的时候
        * 3.touchend      手指离开屏幕的时候
        * 4.touchcancel   ** 被迫终止滑动离开  (系统终止)
        * */
 
        /*怎么样去绑定这些事件*/
        var box = document.querySelector('.box');
 
        /*绑定touchstart*/
        box.addEventListener('touchstart',function(e){
            console.log('touchstart');
            console.log(e);
        });
 
        /*绑定touchmove*/
        box.addEventListener('touchmove',function(e){
            console.log('touchmove');
            console.log(e);
        });
 
        /*绑定touchend*/
        box.addEventListener('touchend',function(e){
           console.log('touchend');
            console.log(e);
        });
 
 
        /*
        * targetTouches 当前目标元素的所有触摸点的集合
        * touches  当前屏幕的所有触摸点集合
        * changedTouches 当前改变的触摸点集合
        *
        * **注意** 在touchend事件当中是没有 targettouches touches 但是有changedtouches
        *
        * */
 
 
        /*每一个触摸点有一些属性*/
        /*clientX:触摸目标在视口中的X坐标。
        clientY:触摸目标在视口中的Y坐标。
        pageX:触摸目标在页面中的x坐标。
        pageY:触摸目标在页面中的y坐标。
        screenX:触摸目标在屏幕中的x坐标。
        screenY:触摸目标在屏幕中的y坐标。*/
 
 
 
    </script>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值