2010-06-18 46 views
3

這個腳本在除谷歌瀏覽器之外的所有瀏覽器都能很好地工作。jQuery && Google Chrome

$(document).ready(function(){ 
    $(".banners-anim img").each(function(){ 
     var hover_width = $(this).width(); 
     var hover_height = $(this).height(); 
     var unhover_width = (hover_width - 30); 
     $(this).width(unhover_width); 
     var unhover_height = $(this).height(); 
     $(this).closest("li").height(unhover_height); 
     var offset = "-" + ((hover_height - unhover_height)/2) + "px"; 
     $(this).closest("span").css({'position':'absolute', 'left':'0', 'top':'25px', 'width':'100%'}); 
     $(this).hover(function(){ 
      $(this).animate({width: hover_width, marginTop: offset}, "fast") 
     },function(){ 
      $(this).animate({width: unhover_width, marginTop: 0}, "fast") 
     }); 
    }); 
}); 

Chrome無法識別更改的圖像屬性。

width的img變化時,height也發生變化。即使無法在Chrome ..

$(this).width(unhover_width); 
var unhover_height = $(this).height(); 

unhover_height0。這個腳本的

的完整代碼(包括HTML) - http://jsfiddle.net/BsqTe/

請幫助解決這個問題。

謝謝。

+0

後,您可能想看看這個問題,一些功能DoResize叫做:http://stackoverflow.com/questions/3035635/google-chrome-is - 做錯了 - 再次 – ig0774 2010-06-18 14:21:51

+0

OT,但所有'$(this)'都讓我變成了wince。你調用一個函數來構造一個對象,然後使用它,然後扔掉它;然後調用構造一個對象的函數,然後拋棄它;然後調用一個函數......你明白了。嘗試'var $ this = $(this)'在頂部,然後使用'$ this'來代替。 – 2010-06-18 14:25:59

+0

@ T.J。 Crowder - 這是一個好主意,謝謝。 – Happy 2010-06-18 14:30:07

回答

7

如果您正在處理jQuery ready函數中的圖像,則需要記住圖像可能尚未加載。 jQuery ready函數的目的是在DOM準備就緒後立即觸發,即使圖像仍在加載。如果你想在所有圖像都已完成加載做一些事情,用windowload事件,而不是:

$(window).load(yourFunctionHere); 
+2

我愛你男人! – Happy 2010-06-18 14:29:02

0

您可能要注意到,圖像也已爲onload功能。此外,一些IE版本有一個特殊的地方,如果圖像已經加載,它不會觸發事件後附加到事件的任何onload函數。所以這個小片段應始終確保圖像已加載

var DoResize = function() { ... } 
var img = $(".find .your img"); 
img.bind('load', DoResize); 
img.bind('error', DoResize); // in case picture fails to load, still resize 
if (img.get(0).complete) { DoResize(); }