获取Ipad touchstart坐标 [英] Obtaining Ipad touchstart coordinates

查看:126
本文介绍了获取Ipad touchstart坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在javascript中获取ipad上触摸事件的坐标。我该怎么做?

I would like to obtain the coordinates of a touch event on the ipad in javascript. How would I do this?

推荐答案

我相信这应该可以解决问题:

I believe this ought to do the trick:

var x = event.targetTouches[0].pageX,
    y = event.targetTouches[0].pageY;



更新:

以下是一个示例:


Update:
Here is an example:

<!doctype html>
<html lang="en" class="no-js">
<head>
    <meta charset="utf-8">

    <title>Touch event test</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>

<body>

<script>
    $(function() {
        var $log = $("#log");

        function updateLog(x, y) {
            $log.html('X: '+x+'; Y: '+y);
        }

        document.addEventListener('touchstart', function(e) {
            updateLog(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
        }, false);

        document.addEventListener('touchmove', function(e) {
            e.preventDefault();
            updateLog(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
        }, false);
    });
</script>

<div id="log"></div>

</body>
</html>

这篇关于获取Ipad touchstart坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆