1. 主页
  2. 文档
  3. 移动之高效开发
  4. 移动端事件
  5. 2.2 touch事件的event对象

2.2 touch事件的event对象

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>2.2 touch事件的event对象</title>
    <style>
        body {
            height: 2000px;
        }
        .box {
            width: 150px;
            height: 150px;
            background-color: red;
            margin: 20px auto;
        }
    </style>
</head>
<body>

    <div id="box" class="box"></div>

    <script>
        var boxEl = document.getElementById('box');

        boxEl.addEventListener('touchstart', handleStart, false);
        boxEl.addEventListener('touchmove', handleMove, false);
        boxEl.addEventListener('touchend', handleEnd, false);

        function handleStart(ev) {
            console.log('touchstart', ev);
            // console.log(ev.touches, ev.targetTouches, ev.changedTouches);
  //打印出event对象

            var touch = ev.changedTouches[0];

            console.log(touch.pageX, touch.pageY);
        }
        function handleMove(ev) {
            console.log('touchmove');
        }
        function handleEnd(ev) {
            console.log('touchend', ev);
            // console.log(ev.touches, ev.targetTouches, ev.changedTouches);
        }
    </script>
</body>
</html>
这篇文章对您有用吗?

我们要如何帮助您?