如何找到元素的x中心坐标和相关的窗口偏移量 [英] How to find the element's x center coordinates and related window offset

查看:101
本文介绍了如何找到元素的x中心坐标和相关的窗口偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i would like to retrieve the element offset starting from his own x center coordinates.

我该怎么做?

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

Actually i can find the window offset of an element but it retrieves the coordinates from the border of the element like this:

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


推荐答案

你必须使用抵消()获取顶部和左侧位置,然后添加一半 height() width()给他们的价值。这给出了中心坐标。

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();

这篇关于如何找到元素的x中心坐标和相关的窗口偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆