You have to use offset()
to get the top and left position, then add half of the height()
and width()
values to them. That gives the center coordinates.
var $this = $(this);
var offset = $this.offset();
var width = $this.width();
var height = $this.height();
var centerX = offset.left + width / 2;
var centerY = offset.top + height / 2;
If you need to consider the padding property in your calculations, use the following:
var width = $this.outerWidth();
var height = $this.outerHeight();