javascript获得鼠标的坐标值

var x , y;

 

//当需求为获得的坐标值相对于body时,用: 

function positionBody(event){

 

    event = event||window.event; 

    //获得相对于body定位的横标值;

 

    x=event.clientX 

    //获得相对于body定位的纵标值; 

    y=event.clientY 

}

 

  

 

//当需求为获得的坐标值相对于某一对象时,用: 

function positionObj(event,id){

 

    //获得对象相对于页面的横坐标值;id为对象的id 

    var thisX = document.getElementById(id).offsetLeft;

 

    //获得对象相对于页面的横坐标值; 

    var thisY = document.getElementById(id).offsetTop;

 

    //获得页面滚动的距离; 

    //注:document.documentElement.scrollTop为支持非谷歌内核;document.body.scrollTop为谷歌内核 

    var thisScrollTop = document.documentElement.scrollTop + document.body.scrollTop;

 

    event = event||window.event; 

    //获得相对于对象定位的横标值 = 鼠标当前相对页面的横坐标值 - 对象横坐标值;

 

    x = event.clientX - thisX;

 

    //获得相对于对象定位的纵标值 = 鼠标当前相对页面的纵坐标值 - 对象纵坐标值 + 滚动条滚动的高度; 

    y = event.clientY - thisY + thisScrollTop; 

  

}
原文地址:https://www.cnblogs.com/dkwlxq/p/2957532.html