Sauf si vous avez besoin de votre largeur et la hauteur pour changer, vous ne devez régler qu'une seule fois, en supposant les deux images sont de la même taille que cela devrait fonctionner
var h=$(".extend").height();
var w=$(".extend").width();
$(".extend").hover(function() {
//alert(h);
$(this).css({'z-index' : '999' });
$(this).addClass("hover").filter(":not(:animated)").stop()
.animate({
marginTop: '-110px',
marginLeft: '10px',
top: '80%',
left: '80%',
width: 387,
height: 487,
padding: '0px'
}, 200);
} , function() {
$(this).css({'z-index' : '0'});
$(this).removeClass("hover").stop()
.animate({
marginTop: '0',
marginLeft: '0',
top: '0',
left: '0',
width:w,
height:h,
padding: '0px'
}, 400);
});
FIDDLE
EDIT--
Pour imag es avec des dimensions différentes de les enregistrer dans l'élément à l'aide .data()
$(".extend").each(function(){
$(this).data('width', $(this).width());
$(this).data('height', $(this).height());
})
$(".extend").hover(function() {
//alert(h);
$(this).css({'z-index' : '999' });
$(this).addClass("hover").filter(":not(:animated)").stop()
.animate({
marginTop: '-110px',
marginLeft: '10px',
top: '80%',
left: '80%',
width: 387,
height: 487,
padding: '0px'
}, 200);
} , function() {
$(this).css({'z-index' : '0'});
$(this).removeClass("hover").stop()
.animate({
marginTop: '0',
marginLeft: '0',
top: '0',
left: '0',
width:$(this).data('width'),
height:$(this).data('height'),
padding: '0px'
}, 400);
});
FIDDLE
EDIT-2-
Une façon simple de résoudre le problème de décalage est d'envelopper les balises img dans un inline- élément de bloc, fixer les dimensions des éléments à celle de l'image pour effectuer le img absolutly positionné
<span><img class="extend" src="a.jpg"></span>
<span><img class="extend" src="b.jpg"></span>
$(".extend").each(function(){
$(this).data('width', $(this).width());
$(this).data('height', $(this).height());
$(this).parent().css({display:'inline-block', width: $(this).width(), height: $(this).height()})
.end().css({position:'absolute'});
})
$(".extend").hover(function() {
//alert(h);
$(this).css({'z-index' : '999' });
$(this).addClass("hover").filter(":not(:animated)").stop()
.animate({
marginTop: '-110px',
marginLeft: '10px',
top: '80%',
left: '80%',
width: 387,
height: 487,
padding: '0px'
}, 200);
} , function() {
$(this).css({'z-index' : '0'});
$(this).removeClass("hover").stop()
.animate({
marginTop: '0',
marginLeft: '0',
top: '0',
left: '0',
width:$(this).data('width'),
height:$(this).data('height'),
padding: '0px'
}, 400);
});
FIDDLE
les deux images sont différentes taille – user1397840
Cela fonctionne mais il n'a pas encore résolu le problème d'alignement lorsque l'image se développer. L'élément à côté de l'image changera à mesure que l'image s'élargira. Comment le résoudre? Merci d'avance. – user1397840
@ user1397840 voir mon edit – Musa