首页 > 手机端:touchend触发时,如何获取到触点离开屏幕时的pageX

手机端:touchend触发时,如何获取到触点离开屏幕时的pageX

请问一下,为什么touchend事件触发时,获取不到pageX。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1">
<title>touchend获得不到pageX吗?</title>
<style>
*{margin: 0; padding: 0;}
div{width: 320px; height: 160px;
    background: orange;
}
</style>
</head>
<body>
    <div></div>
<script>
window.onload=function(){ 
    var oD=document.querySelector('div');
    oD.addEventListener('touchstart',function(ev){ 
        console.log(ev.targetTouches[0].pageX);
    },false);
    oD.addEventListener('touchend',function(ev){ 
        // console.log(ev.changedTouches[0].pageX);这个可以获取到pageX
        console.log(ev.targetTouches[0].pageX); // 谷歌:Uncaught TypeError: Cannot read property 'pageX' of undefined
    },false);
};    
</script>
</body>
</html>

运行结果:


e.touches[0].pageX

在touchstart时候获取pageX,然后在获取touchmove的pageX,就能计算出touchend

【热门文章】
【热门文章】