简体   繁体   中英

How to get position of a square div jQuery/JavaScript

setInterval(function() {
  var divPosition = $('div').position();


  console.log('X: ' + divPosition.left + ", Y: " + divPosition.top");
}, 500);

So I can get the x and y position of this div. left / top but it's a square div on the page. I'm also tracking a section tag that flies around the page, I want to basically do if (_thesectiontag_.left == _thesquarediv_.left || _thesectiontag_.top == _thesquarediv_.top) ... do something so if the section tag is within the div coordinates on the page do something.

But I need to get the full dimensions of the square to be able to do that. I'm a bit lost on where to start and how to go about it.

Can anyone offer some help? Thank you!

Use this two code :

For the Width and Height includes padding :

var Height = document.getElementById('square').clientHeight;

var Width = document.getElementById('square').clientWidth;

For the Width and Height includes padding, scrollBar and borders :

var Height = document.getElementById('square').offsetHeight;

var Width = document.getElementById('square').offsetWidth;
$('div').height($('div').width());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM