为什么 jQuery .css('left') 和 .position().left 返回不同的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11860130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:00:41  来源:igfitidea点击:

Why does jQuery .css('left') and .position().left return different values?

jquerygoogle-chromejquery-animate

提问by Gaurav Dadhania

The values I'm getting for $(el).css('left')and $(el).position().leftare different?

我得到的价值观$(el).css('left')$(el).position().left不同?

If I go $(el).css('left', '100px'), then $(el).css('left')it returns 110pxinstead of 100px(yes, it is always 10% more) and if I evaluate $(el).position().left, it gives me 100.

如果我去$(el).css('left', '100px'),那么$(el).css('left')它会返回110px而不是100px(是的,它总是多 10%),如果我评估$(el).position().left,它会给我100.

Why does Chrome behave this way? You can see how this would affect jQuery animations using the leftproperty.

为什么 Chrome 会这样?您可以使用该left属性查看这将如何影响 jQuery 动画。

I'm using Chrome 21.0.1180.57 on Ubuntu.

我在 Ubuntu 上使用 Chrome 21.0.1180.57。

EDIT 1: Seems to be only affecting Chrome, FF 14.0.1 is giving me the same values.

编辑 1:似乎只影响 Chrome,FF 14.0.1 给了我相同的值。

回答by Esailija

The leftreturns calculated value of the CSS left property.

left对CSS的回报率计算值left属性。

position().leftreturns the x-coordinate relative to the element's first offset parent.

position().left返回相对于元素的第一个偏移父元素的 x 坐标。

These values can be equaland they can be not equal.

这些值可以相等,也可以不相等

position().leftcan also easily be different between browsersbecause of different rendering whereas .css("left")can only differ in the units given, if that.

position().left由于不同的渲染,浏览器之间也很容易不同,.css("left")只能在给定的单位上有所不同,如果那样的话。

回答by Gabriel Silveira

Try this code :

试试这个代码:

$("#div")[0].style.left