Home  >  Article  >  Web Front-end  >  js method to get the coordinate value of the mouse_javascript skills

js method to get the coordinate value of the mouse_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:40:43818browse

Copy code The code is as follows:

var x , y;

//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;

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn