애니메이션 효과 만드는 방법 - .animate()

animate는 애니메이션 효과를 만듭니다. 애니메이션 효과를 줄 속성을 정합니다.
3 min read

.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 );
    });
  });
});

You may like these posts

  • .before() .before()는 선택한 요소 앞에 새 요소를 추가하거나, 다른 곳에 있는 요소를 이동시킬 수 있습니다.   기본형 .before( content [, content ] ) 예를 들어 $( 'h1' ).before( '<p>Hello</p>'…
  • .children() .children()은 어떤 요소의 자식 요소를 선택합니다.   기본형 .children( [ selector ] ) 예를 들어 $( 'div' ).children().css( 'color', 'blue' ); 는 <div> 요소의 자식 요소의 색을 파란색으…
  • .click() .click()은 선택한 요소를 클릭했을 때 특정 작업을 수행할 수 있게 하는 속성입니다.   기본형 .click( handler ) 예를 들어 button 요소를 클릭했을 때 함수를 실행시키고 싶으면 다음과 같이 합니다. $( 'button' ).clic…
  • .append() .append()는 선택한 요소의 내용의 끝에 콘텐트를 추가합니다.   기본형 .append( content [, content ] )   예를 들어 <p>Lorem Ipsum Dolor</p> 가 있을 때 $( 'p' ).appen…
  • .attr() .attr()은 요소(element)의 속성(attribute)의 값을 가져오거나 속성을 추가합니다.   기본형 1 .attr( attributeName ) 선택한 요소의 속성의 값을 가져옵니다. 예를 들어 $( 'div' ).attr( 'class' ); 는…
  • .appendTo() .appendTo()는 선택한 요소를 다른 요소 안에 넣습니다.   기본형 .appendTo( target ) 예를 들어 $( 'p' ).appendTo( 'blockquote' ); 는 <p> 요소를 <blockquote> 요소 안으로 …

Post a Comment