幅と高さを変更する必要がない限り、両方の画像が同じサイズであると仮定して、一度だけ設定する必要があります。
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);
});
フィドル
編集-異なるサイズの画像の場合は、 .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);
});
フィドル
編集-2-
シフトの問題を解決する簡単な方法は、imgタグをインラインブロック要素でラップし、要素の寸法を画像の寸法に設定してから、imgを絶対的に配置することです。
<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);
});
フィドル