2012-11-22 127 views
1

很奇怪jquery.offset()方法的行为

$view.offset({ 
    left : X, //X is the same each time 
    top : this.y 
}); 
console.log($view.offset()); //outputs what it should 

好对象后。我看到(在萤火虫)以下HTML代码

<div id="4017" class="text-block" style="position: relative; top: 2px; left: 22px;"> 
<div id="4043" class="text-block" style="position: relative; top: 41px; left: -64px;"> 
<div id="4053" class="text-block" style="position: relative; top: 80px; left: -95px;"> 
<div id="4081" class="text-block" style="position: relative; top: 119px; left: -135px;"> 

left应该是所有div一样(和它的显示,所以如果left等于每个div)。为什么left对于每个div都不相同,尽管它显示为left对于所有div都是相同的?

在CSS我:

div.text-block { 
    display: inline-block; 
} 

预先感谢您!

UPD:的div位于iside其他三个的div:

<div id="app-container"> 
    <div id="canvas-container"> 
     <div id="canvas"> 
      <!-- divs are located here --> 
     </div> 
    </div> 
</div> 

在各自CSS我有:

#canvas { 
    position: absolute; 
    background-color: white; 
    width: 100%; 
    height: 100%; 
} 

#app-container { 
    height: auto !important; 
    min-height: 100%; 
} 

#canvas-container { 
    position: absolute; 
    width: 100%; 
    height: 100%; 
    overflow: hidden; 
} 
+0

我们能有您的问题的小提琴,所以我们可以看到更多的代码 – Bruno

+0

中的“X”的价值正在改变,但我们不能说,除非你发布更多的代码 –

回答

1

如图jQuery documentation所述,

.offset(坐标)

描述:设置匹配元素集合中每个元素相对于文档的当前坐标。

因此,如果您的元素位于相对于文档的位置(0,0)上的其他元素中,则会应用偏移量。


编辑

与上相同的左值相对定位内联元素:

Demo picture

+0

谢谢!我已经更新了这个问题,也许这可以帮助你理解这是什么 – Eugeny89

+0

是的,因为你的'div'被声明为'inline-block',并且相对定位,所以你的原点被移动到一个块的末尾已经占用了你之前的块的长度。我知道这并不是很清楚,但我添加了一张图片,我已经强制将所有“left”位置设置为相同的值... –

+1

顺便说一句,如果您想要在代码中拥有相同的左侧位置,您可以写:'$ view.css({position:“relative”,left:X,top:this.y});'... –