如何找到元素的x中心坐标和相关的窗口偏移量

我想从他自己的x中心坐标开始检索元素偏移量。

我该怎么做?

实际上我可以找到元素的窗口偏移量,但它从元素的边框检索坐标,如下所示:

var _position = $(this).offset(); 

您必须使用offset()来获取顶部和左侧位置,然后将一半height()width()值添加到它们。 这给出了中心坐标。

 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; 

现在可以通过本机Javascript完成:

 let centerX = targetNode.offsetLeft + targetNode.offsetWidth / 2; let centerY = targetNode.offsetTop + targetNode.offsetHeight / 2; 

其中targetNode是您想要获取其中心坐标的元素。