我有以下代码:
$('.active-bean-bag ul li').hover(function() {
var activeBeanBag = 'menu-' + $(this).attr("class");
$('.active-bean-bag img.menu-image').fadeOut();
$('.active-bean-bag img.menu-image').fadeIn(function(){
$('.active-bean-bag img.menu-image').attr("src", '/images/'+activeBeanBag+'.png');
});
});
但是,当我将鼠标悬停在不同的LI
位置上时,它看起来像是在旧图像上淡出两次,然后淡入淡出,然后淡出增益然后淡入新图像?任何想法为什么?
更新:
$('.active-bean-bag ul li').mouseover(function() {
var activeBeanBag = 'menu-' + $(this).attr("class");
var newImage = '/skins/temaplate/customer/images/'+activeBeanBag+'.png';
var $img = $('.active-bean-bag img.menu-image');
if ($img.attr('src') != newImage) {
$img.fadeOut(function() {
$(this).attr("src", newImage).fadeIn();
});
}
});
也尝试了相同的结果:
$(".active-bean-bag ul li").hover(function(e) {
e.preventDefault();
var activeBeanBag = 'menu-' + $(this).attr("class");
$('.active-bean-bag img.menu-image').fadeOut(400, function() {
$('.active-bean-bag img.menu-image').attr('src','/skins/template/customer/images/'+activeBeanBag+'.png');
}).fadeIn(400);
});