This article mainly introduces how to obtain div coordinates using js. It is very simple and practical to call the jQuery plug-in to obtain div element coordinates, for more information about how to obtain pcoordinates in js, see the example in this article. We will share this with you for your reference. The details are as follows:
The most commonly used control in html is p. How can I obtain the coordinates of p?
The following methods can be implemented:
/***** Obtain the coordinates of p * @ param pObj * @ returns {width: number, height: number, left: *, top: Window} */com. whuang. hsj. pCoordinate = function (pObj) {if (typeof pObj = 'string') {pObj = com. whuang. hsj. $ id ('pobj ');} return {'width': pObj. offsetWidth, 'height': pObj. offsetHeight, 'x': pObj. offsetLeft, 'y': pObj. offsetTop, 'rollleft': com. whuang. hsj. getScroll (). left, 'rolltop ': com. whuang. hsj. getScroll (). top };}// Cross browser gets the position of scrollcom. whuang. hsj. getScroll = function () {return {top: document.doc umentElement. scrollTop | document. body. scrollTop, left: document.doc umentElement. scrollLeft | document. body. scrollLeft }}
Introduction to the com. whuang. hsj. pCoordinate () method
Function: returns the pcoordinate;
Parameter: p object or p id (string );
Returned value: object, which has six attributes:
Width: the width of p;
Height: the height of p;
X: coordinate x in the upper left corner of p;
Y: y in the upper left corner of p;
ScrollLeft: horizontal scroll bar position
ScrollTop: the position of the vertical scroll bar
Test page: