Obtaining Ipad touchstart coordinates

两盒软妹~` 提交于 2019-12-22 05:45:10

问题


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


回答1:


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>



回答2:


Try to use:

x = event.originalEvent.pageX;
y = event.originalEvent.pageY;



回答3:


This is an old question, but there is no accepted answer and the solution in my case with the iPad was below:

var x = event.originalEvent.touches[0].clientX;
var y = event.originalEvent.touches[0].clientY;



回答4:


Try this js fiddle..Properly working in all major browsers and all touch devices. In this code i'm providing how to find touchstart coordinates.

http://jsfiddle.net/vecny/



来源:https://stackoverflow.com/questions/6228800/obtaining-ipad-touchstart-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!