码迷,mamicode.com
首页 > 移动开发 > 详细

js获取鼠标移动坐标

时间:2017-03-31 18:15:14      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:page   function   logs   lis   client   mouse   tar   cti   eve   

js获取鼠标移动坐标,很多情况下还需要考虑document.documentElement.scrollLeft 、 document.documentElement.clientLeft;

    var _touchstart, _touchend,_touchmove;
    var isPC=window.ontouchstart===undefined;
    if(isPC){
        _touchstart="mousedown";
        _touchmove="mousemove";
        _touchend="mouseup";
    }else{
        _touchstart="touchstart";
        _touchmove="touchmove";
        _touchend="touchend";
    }
    var sx,sy,ex,ey;
    document.addEventListener(_touchstart,function(event){
        if(isPC){
            sx=event.clientX;
            sy=event.clientY;
        }else{
            sx=event.targetTouches[0].pageX;
            sy=event.targetTouches[0].pageY;
        }
    },false);
    document.addEventListener(_touchmove,function(){
        if(isPC){
            ex=event.clientX;
            ey=event.clientY;
        }else{
            ex=event.targetTouches[0].pageX;
            ey=event.targetTouches[0].pageY;
        }
    },false);
    document.addEventListener(_touchend,function(){
        if(isPC){
            ex=event.clientX;
            ey=event.clientY;
        }else{
            ex=event.targetTouches[0].pageX;
            ey=event.targetTouches[0].pageY;
        }
    },false);

 

js获取鼠标移动坐标

标签:page   function   logs   lis   client   mouse   tar   cti   eve   

原文地址:http://www.cnblogs.com/mrGan/p/6652011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!