2011-10-12 62 views
1

我有一個span$mySpan固定位置;左工作正常,對不對

調用

$mySpan.css({ 
    position  : 'fixed', 
    left   : '50%', 
    top    : '15%', 
    backgroundColor : 'red', 
    'z-index'  : 1000 
}); 

的作品就好了。但是,

$mySpan.css({ 
    position  : 'fixed', 
    right   : '50%', 
    top    : '15%', 
    backgroundColor : 'red', 
    'z-index'  : 1000 
}); 

不顯示跨度。

正如你所看到的,這可能不是z-index問題。我非常困惑,因爲我的代碼非常複雜,我一直無法使用jsFiddle構建一個簡單的示例。我不知道從哪裏開始尋找哪裏出了問題。

有沒有人有暗示,爲什麼第二種方法不工作?我怎麼能調試這樣的問題?

+0

是缺少'$'一個錯字,還是? – thirtydot

+0

第二個代碼片段中是否缺少'$',這裏只是一個錯字,或者您是否在實際代碼中有這樣的錯誤? – gilly3

+1

錯字只...在代碼中是正確的 – Randomblue

回答

3

試試這個:

$mySpan.css({ 
    'position'  : 'fixed', 
    'left'   : 'auto', /* <-- oftentimes, browsers screw up if 'left' is still set */ 
    'right'   : '50%', 
    'top'    : '15%', 
    'backgroundColor' : 'red', 
    'z-index'   : 1000 
}); 
+0

這可能是答案。 – thirtydot

+0

@vzwick,如果'left'屬性必須有值呢?如果我讓'left:'auto'',我的'div'會將其內容溢出到瀏覽器的左側。你認爲這是一個瀏覽器錯誤? (我在Chrome上測試過) – TrungDQ

+0

@TrungDQ - 小提琴會有幫助;) – vzwick

1

意外結果可能是由margin-right CSS屬性造成的。如果此屬性已設置,請將其刪除以修復您的代碼。

+0

控制檯中沒有任何錯誤消息,並且我沒有在實際代碼中發生錯誤。 – Randomblue

+0

@Randomblue更新了可能原因的答案。 –

0

在第二個例子中,您可能錯過了$$mySpan

+0

我沒有在我的實際代碼中犯這個錯誤。 – Randomblue

+0

所以[this fiddle](http://jsfiddle.net/q2vPa/)不起作用? –