1. 主页
  2. 文档
  3. 移动之高效开发
  4. 移动端事件
  5. 1-1 移动端事件介绍

1-1 移动端事件介绍

<!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.1 touch事件基础</title>
    <style>
        .box {
            width: 150px;
            height: 150px;
            background-color: red;
            margin: 20px auto;
        }
    </style>
</head>
<body>
    <div id="box" class="box"></div>

    <script>

       //box.onclick = fun;
       //addEventListener();
        var boxEl = document.getElementById('box');

        // boxEl.ontouchstart = handleStart;
        // boxEl.ontouchmove = handleMove;
        // boxEl.ontouchend = handleEnd;
        // boxEl.ontouchcancel = handleCancel;

        boxEl.addEventListener('touchstart', handleStart, false);   //触摸开始
        boxEl.addEventListener('touchmove', handleMove, false);//触摸移动
        boxEl.addEventListener('touchend', handleEnd, false);//触摸结束

        function handleStart() {
            console.log('touchstart');
        }
        function handleMove() {
            console.log('touchmove');
        }
        function handleEnd() {
            console.log('touchend');
        }
    </script>
</body>
</html>
这篇文章对您有用吗?

我们要如何帮助您?