我的保證金值在這裏設置爲零,我不明白爲什麼。
$(document).ready(function() {
$('img.thumbNormal').each(function(){
$(this).css({'position':'relative', 'left':'50%', 'margin-left':-$(this).width()/2});
});
});
如果我在控制檯中運行相同的行,結果是你所期望的。
任何幫助,將不勝感激。謝謝!
我的保證金值在這裏設置爲零,我不明白爲什麼。
$(document).ready(function() {
$('img.thumbNormal').each(function(){
$(this).css({'position':'relative', 'left':'50%', 'margin-left':-$(this).width()/2});
});
});
如果我在控制檯中運行相同的行,結果是你所期望的。
任何幫助,將不勝感激。謝謝!
您需要在圖像載入後執行此操作。
$(window).load(function() {
$('img.thumbNormal').each(function(){
$(this).css({'position':'relative', 'left':'50%', 'margin-left':-$(this).width()/2});
});
});
你正在做它$(document).ready()
,不等待圖像加載和圖像的.width()
是0
,直到他們完全加載。
謝謝一堆。 – 2011-03-01 18:59:45
+1從未見過那些被映入腦海的影像:) – Sarfraz 2011-03-01 19:11:08
是否有任何其他代碼或樣式表可能會產生干擾? – justkt 2011-03-01 18:41:11
我想你甚至可以省略。因爲'$('img.thumbNormal').css(bla)'已經可以解決這個問題了。 – Marnix 2011-03-01 18:43:49
代碼對我來說看起來很好,並且在這裏可以很好地工作(http://jsfiddle.net/Wkseg/),您可能會遇到其他代碼/ css衝突。 – McHerbie 2011-03-01 18:44:36