2014-04-07 52 views
0

我有一個腳本,可以在懸停時更改某些圖像。工作得很好,但是我想在兩張圖像之間添加一點淡入淡出。這裏是腳本。真的很新的jQuery,所以我有點困惑在哪裏添加它。任何幫助,將不勝感激帶有淡入淡出功能的jQuery切換圖像

jQuery(document).ready(function ($) { 
    var img_src = ""; 
    var new_src = ""; 
    $(".rollover").hover(function() { 
     img_src = $(this).attr('src'); 
     new_src = $(this).attr('rel'); 
     $(this).attr('src', new_src); 
     $(this).attr('rel', img_src); 
    }, function() { 
     $(this).attr('src', img_src); 
     $(this).attr('rel', new_src); 
    }); 
    //preload images 
    var cache = new Array(); 
    //cycle through all rollover elements and add rollover img src to cache array 
    $(".rollover").each(function() { 
     var cacheImage = document.createElement('img'); 
     cacheImage.src = $(this).attr('rel'); 
     cache.push(cacheImage); 
    }); 

回答

1
function imageSwitch(img) { 
    var src = img.attr('src'); 
    var rel = img.attr('rel'); 
    img.fadeOut('fast', function() { 
      img.attr('src', rel); 
      img.attr('rel', src); 
      img.fadeIn('fast'); 
    }); 
} 

$(".rollover").on('mouseenter', function() { 
     imageSwitch($(this)); 
}); 
+0

謝謝!它看起來不錯,但對不起,我應該更好地解釋這一點。 相反,只是淡出它然後淡入,有沒有辦法過渡這兩個圖像? – Jordan

+0

您可以將兩個圖像一個放在另一個的上面。然後淡出頂部。 – ChrisBarthol

0

這將是更好地使用.mouseenter這一點。

您可以將.animate()不透明度設置爲0,並且在回調函數中您將更改圖像src,以便在動畫結束後更改src。

$('.rollover').mouseenter(function() { 
    var me = $(this), 
    new_src = me.attr('rel'), 
    anmiation_duration = 300; // 300ms 
    me.animate({ 
    opacity: 0 
    }, anmiation_duration, function() { 
    me.attr('src', new_src).css({'opacity':'1'}); 
    }) 
}); 

後在src改變,你可以做另一個.animate()來設置不透明度回到1

但我會建議已經顯示後面的第一個圖像(圖像的其他圖像使用位置:absolut放置在彼此之上)。這樣不透明度的改變會產生一種形式效應。

+0

關於'mouseenter'的好消息。編輯。 –

0

經過簡短測試,感謝@Oksidmouseenter的評論。

EDITED

的HTML:

<img class="front rollover" src="http://placehold.it/350x150/006699" rel="http://placehold.it/350x150/000000" /> 

的CSS:

.image_holder { 
    position: relative; 
} 

.image_holder img { 
    position: absolute; 
    top: 0; 
    left: 0; 
    z-index: 1; 
} 

.image_holder img.front { 
    z-index: 2 !important; 
} 

jQuery的:

function init_rollovers() { 
    $(".rollover").each(function() { 
     var w = $(this).width()+'px'; 
     var h = $(this).height()+'px'; 
     var src = $(this).attr('src'); 
     var rel = $(this).attr('rel'); 
     $(this).addClass('shown'); 
     if (!$(this).parents('.image_holder').length) { 
      $(this).wrap('<div class="image_holder" style="width:'+w+';height:'+h+';"></div>'); 
      $(this).parents('.image_holder').append('<img src="'+rel+'" />'); 
     } 
    }); 
} 

init_rollovers(); 

function doImageSwitch(el, speed=425) { 
    var front = el.find(".front"); 
    if (front.hasClass('shown')) { 
     front.fadeTo(speed, 0, function() { 
      $(this).removeClass('shown'); 
     }); 
    } 
    else { 
     front.animate({ 
      opacity: 1 
     }, speed, function() { 
      $(this).addClass('shown'); 
     }); 
    } 
} 

$(document).on('mouseenter', '.image_holder', function() { 
    doImageSwitch($(this)); 
}); 
+0

很酷,謝謝!唯一的問題是,它會將我的所有圖像移動到標記的底部?就在我添加rollover.js的時候,而不是圖片實際上在標記中的位置? http://i57.tinypic.com/70jlls.png – Jordan

+0

我更新了使用jQuery的'wrap()'方法的答案,該方法允許您只包裝圖像,而不指定容器。 –