How to Get div coordinates using js _ javascript skills

Source: Internet
Author: User
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:

  
     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.