Home >Web Front-end >JS Tutorial >js method to get the coordinate value of the mouse_javascript skills
//When the requirement is to obtain the coordinate value relative to the body, use:
function positionBody(event){
event = event||window.event;
//Get the horizontal value positioned relative to the body;
x=event.clientX
//Get the vertical scale value positioned relative to the body;
y=event.clientY
}
//When the requirement is to obtain the coordinate value relative to an object, use:
function positionObj(event,id){
//Get the abscissa value of the object relative to the page; id is the id of the object
var thisX = document.getElementById(id).offsetLeft;
//Get the abscissa value of the object relative to the page;
var thisY = document.getElementById(id).offsetTop;
//Get the page scroll distance;
//Note: document.documentElement.scrollTop supports non-Google kernel; document.body.scrollTop supports Google kernel
var thisScrollTop = document.documentElement.scrollTop document.body.scrollTop;
event = event||window.event;
//Get the horizontal coordinate value relative to the object positioning = the current horizontal coordinate value of the mouse relative to the page - the horizontal coordinate value of the object;
x = event.clientX - thisX;
//Get the vertical coordinate value relative to the object positioning = The vertical coordinate value of the mouse currently relative to the page - The vertical coordinate value of the object The height of the scroll bar;
y = event.clientY - thisY thisScrollTop;
}