如何最好地响应“滑动”触摸移动事件?

时间:2019-12-18 22:41:53

标签: javascript jquery html dom

我想在小型在线游戏中使用“滑动”触摸动作(向上滑动,向左滑动等)。有谁知道最好的方法?

我搜索了触摸事件,并找到了一个有用的页面: https://www.w3schools.com/jsref/obj_touchevent.asp

我还发现了如何在html元素上获得touchmove事件的坐标:

<html>
    <head>
        <title>DOM Touchmove Event</title>
    </head>
    <body>
        <h1>DOM Touchmove Event</h1>

        <p ontouchmove="myFunction(event)">Touch this paragraph, and move the finger to trigger a function that will display the x and y coordinates of the touch, according to the document:</p>

<p><strong>Note:</strong> This expmple is for touch devices only.</p>

<p id="demo"></p>

<script>
    function myFunction(event) {
        var x = event.touches[0].clientX
        var y = event.touches[0].clientY
        document.getElementById("demo").innerHTML = x + ", " + y
    }
</script>
    </body>
</html>

0 个答案:

没有答案