마우스가 움직 인 후에 다시. 상태를 저장하는 객체를 만드는 것이 아마도 최상의 솔루션 일뿐만 아니라 매우 적응력이 좋습니다 (다른 이미지, 다른 유형의 애니메이션). 정확한 타이밍이 중요 경우
http://jsfiddle.net/VceD9/6/
new GrowingImage('myImage', 2, 1000);
function GrowingImage(id, factor, duration) {
var el = document.getElementById(id),
originalWidth = el.offsetWidth,
originalHeight = el.offsetHeight,
timer,
stage = 0,
frameRate = 17,
maxStage = duration/frameRate;
el.onmouseover = function() {
animate(1);
};
el.onmouseout = function() {
animate(-1);
};
function animate(direction) {
clearInterval(timer);
timer = setInterval(function() {
stage += direction;
if (stage <= 0) {
stage = 0;
clearInterval(timer);
} else if (stage >= maxStage) {
stage = maxStage;
clearInterval(timer);
}
var scale = 1 + (factor - 1) * stage/maxStage;
el.style.width = originalWidth * scale + 'px';
el.style.height = originalHeight * scale + 'px';
}, frameRate);
}
}
, 당신은 현재 애니메이션이 실행 된 시간의 양을 추적 있도록이를 조정해야 할 수도 있습니다.
완벽. 바이올린을 포함한 +1. – BAR