
자바스크립트를 이용해서 다음과 같은 이미지를 애니매이션 효과를 주는 스크립트입니다.
물론 gif로 처리할수도 있지만 이와 같은 방법으로 처리 한다는것도 상당히 재미있는 부분일수 있습니다.
script 소스
var Ani = Class.create();
Ani.prototype = {
initialize: function(img, element, options){
this.options = Object.extend({
cnt: 0, // 애니메이션 몇장의 컷인가?
width: 0, // 애니메이션 크기
height: 0, // 애니메이션 높이
line: 0, // 이미지 줄
field: 0, // 이미지 몇개에
x: 0, // 시작 x점
y: 0, // 시작 y점
interval: 0, // 인터발 시간이며, 이 시간은 javascript 의 시간이다. 1초 = 1000
repeat: 0, // 이미지 반복횟수
type: 'image' // 타입은 background, image 형 두개가 있다.
}, options || {});
this.img = img;
this.element = (typeof(element) == 'string') ? $(element) : element;
this.aniNode = document.createElement('div');
Element.setStyle(this.aniNode, {width:this.options.width+'px', height:this.options.height+'px', overflow:'hidden', position:'relative'});
this.element.appendChild(this.aniNode);
this.options.x = this.options.x*-1;
this.options.y = this.options.y*-1;
this[this.options.type]();
this.repeat = 1;
this.cnt = 0;
this.line = 0;
this.field = 0;
this.interVal = setInterval(this.ani.bind(this), this.options.interval);
},
ani: function()
{
var x = y = 0;
if((this.options.cnt-2) < this.cnt)
{
this.cnt = 0;
this.repeat++;
this.line = 0;
this.field = 0;
}
// 리피트가 넘으면 종료
if(this.options.repeat < this.repeat)
{
clearInterval(this.interVal);
return;
}
//alert(this.cnt%this.options.field);
if((this.cnt%this.options.field) == (this.options.field-1))
{
this.line++;
this.field = 0;
}
y = (this.line * this.options.height)*-1;
x = (this.field * this.options.width)*-1;
y = this.options.y+y;
x = this.options.x+x;
if(this.options.type == 'background')
{
this.aniNode.style.backgroundPosition = x+'px '+y+'px';
}
else
{
this.imgNode.style.top = y+'px';
this.imgNode.style.left = x+'px';
}
this.cnt++;
this.field++;
},
background: function()
{
this.aniNode.style.backgroundImage = 'url('+this.img+')';
this.aniNode.style.backgroundPosition = this.options.x+'px '+this.options.y+'px';
},
image: function()
{
this.imgNode = document.createElement('img');
this.imgNode.setAttribute('src', this.img);
Element.setStyle(this.imgNode, {position:'absolute', top:this.options.y, left:this.options.x});
this.aniNode.appendChild(this.imgNode);
}
}
http://www.gudals.com/gostop/animation.html
샘플은 위의 링크에서 확인하실 수 있습니다.
보다 자세한 내용은 소스를 참조하시기 바랍니다.