.animate
.animate()는 애니메이션 효과를 만듭니다.
기본형
.animate( properties [, duration ] [, easing ] [, complete ] )
properties
애니메이션 효과를 줄 속성을 정합니다. 가능한 속성은 다음과 같습니다.
- backgroundPositionX
- backgroundPositionY
- borderBottomWidth
- borderLeftWidth
- borderRightWidth
- borderSpacing
- borderTopWidth
- borderWidth
- bottom
- fontSize
- height
- left
- letterSpacing
- lineHeight
- margin
- marginBottom
- marginLeft
- marginRight
- marginTop
- maxHeight
- maxWidth
- minHeight
- minWidth
- opacity
- outlineWidth
- padding
- paddingBottom
- paddingLeft
- paddingRight
- paddingTop
- right
- textIndent
- top
- width
- wordSpacing
duration
- 애니메이션 효과를 완료할 때까지 걸리는 시간입니다.
- 단위는 1/1000초, 기본값은 400입니다.
- fast나 slow로 정할 수 있습니다.
- fast는 200, slow는 600에 해당합니다.
easing
- 애니메이션 효과의 방식을 정합니다.
- swing과 linear이 가능하며, 기본값은 swing입니다.
complete
- 요소가 사라진 후 수행할 작업을 정합니다.
예제 1
버튼을 클릭하면 파란색 div 요소의 가로 크기가 변합니다.
<p><button>Click</button></p>
<div id="a"></div>
#a {
width: 10%;
height: 100px;
background-color: #bbdefb;
}
$( document ).ready( function() {
$( 'button' ).click( function() {
$( '#a' ).animate( {
width: '100%'
});
});
});
예제 2
swing과 linear를 비교한 예제입니다. linear는 일정한 속도로 진행합니다.
<p><button>Click</button></p>
<div id="a"></div>
<div id="b"></div>
div {
margin: 10px 0px;
width: 10%;
height: 100px;
background-color: #e3f2fd;
}
#a {
background-color: #bbdefb;
}
#b {
background-color: #ffcdd2;
}
$( document ).ready( function() {
$( 'button' ).click( function() {
$( '#a' ).animate( {
width: '100%'
}, 2000, 'swing' );
$( '#b' ).animate( {
width: '100%'
}, 2000, 'linear' );
});
});
예제 3
div 요소가 커지는 애니메이션을 마친 후, 사라지는 애니메이션을 실행합니다.
<p><button>Click!</button></p>
<div id="a"></div>
#a {
width: 10%;
height: 100px;
background-color: #bbdefb;
}
$( document ).ready( function() {
$( 'button' ).click( function() {
$( '#a' ).animate( {
width: '100%'
}, 1000, function() {
$( this ).animate( {
width: '0%'
}, 1000 );
});
});
});