我的目标是在鼠标悬停时将图像设置为新尺寸,并临时添加一个新的 css 类,并在鼠标悬停时将其返回到其原始尺寸(并删除添加的类)。
我的代码在鼠标悬停时工作正常。鼠标移出时,添加的类被删除,但图像尺寸不会恢复到原始宽度和高度。
HTML:
<img src='img.jpg' alt='' class='pulse'>
jQuery:
$(function() {
// set global img width and height
var width_pulse_img = 0;
var height_pulse_img = 0;
$('img.pulse').hover(function () {
// onMouseOver: read original width and height on FIRST mouseover
if (!width_pulse_img) width_pulse_img = $(this).css('width');
if (!height_pulse_img) height_pulse_img = $(this).css('height');
// set img expansion ratio and compute new dimensions
var float_exp = 1 + 0.1;
var ww = (float_exp*parseInt(width_pulse_img, 10)).toString();
var hh = (float_exp*parseInt(height_pulse_img, 10)).toString();
// animate to new dimensions
$(this).addClass('hover').stop().animate({
width: ww + 'px',
height: hh + 'px'
}, 200);
}, function () {
// onMouseOut: animate back to original dimensions:
$(this).removeClass('hover').stop().animate({
// THIS IS THE PART THAT DOES NOT WORK
width: width_pulse_img + 'px',
height: height_pulse_img + 'px'
}, 400);
});
});
我已经在 Firefox 12.0、Chrome 和 IE 9.0 中进行了测试,并且行为是相同的。
这是一个演示问题的jsFiddle。任何帮助将不胜感激。