为什么 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
Why does jQuery .css('left') and .position().left return different values?
提问by Gaurav Dadhania
The values I'm getting for $(el).css('left')
and $(el).position().left
are different?
我得到的价值观$(el).css('left')
和$(el).position().left
不同?
If I go $(el).css('left', '100px')
, then $(el).css('left')
it returns 110px
instead 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 left
property.
为什么 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 left
returns calculated value of the CSS left property.
在left
对CSS的回报率计算值left属性。
position().left
returns 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().left
can 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