如何获得Android + phonegap环境中的触摸坐标? [英] How to get the touch co-ordinates in android+phonegap environment?

查看:115
本文介绍了如何获得Android + phonegap环境中的触摸坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台目标 - Android 4.0及更高版本。

Platform target - android 4.0 and above.

环境 - html5 + css3 + javascript使用phonegap

Environment - html5+css3+javascript using phonegap

在我的应用程序在上面的环境下,我想要一个弹出菜单,只是一个< div> ,出现,当用户触摸它上面的按钮顶部几个像素。到目前为止,我已经完成了这个:

In my app under above environment, I want a popup menu which is nothing but a <div>, to appear, exactly few pixels above a button's top when user touches it. Till now I've worked out upto this:

<head>
<script type="text/javascript" charset="utf-8">

         var touchme=document.getElementById('#button');
         function onDeviceReady(){

                   touchme.addEventListener('touchstart',onNav, false);
         }
         function onNav() {

                popup=document.querySelector("#divPop");
                popup.style.display="block"; //this will make a popup appear
                touchme.getBoundingClientRect();
                navigator.notification.alert(touchme.top);
         }
</script>
</head>

现在的问题是,即使警报没有出现。

Now problem is, even alert is not showing up. So how would I get the co-ordinates of the button?

推荐答案

首先,在DOM加载后尝试该代码 - 您的脚本位于< head> 标签中,并且您试图访问元素 #button

First of all, try that code after the DOM is loaded - your script is in the <head> tag and you're attempting to access the element #button pre-emptively.

var touchme = null;
function onDeviceReady(){
    touchme = document.getElementById('#button');
    touchme.addEventListener('touchstart',onNav, false);
}

window.addEventListener("DOMContentLoaded", onDeviceReady, false);

为坐标,看看screenX和screenY属性的事件到onNav)。要查找的属性与常规鼠标点击的属性基本相同。

as for the coordinates, have a look at the screenX and screenY properties of the event (which is passed in to onNav). The properties to look for are essentially identical to that of a regular mouse click.

function onNav(e) {
    alert(e.screenX + ' ' + e.screenY);
}

这篇关于如何获得Android + phonegap环境中的触摸坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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