순서대로 나타나게 하는 방법

animate로 요소를 순서대로 나타나게 하는 방법 animate를 이용하여 서서히 나타나게 만든 예제입니다.
2 min read

순서대로 요소를 나타내는 방법

.animate()를 이용하여 서서히 나타나게 만든 예제입니다.

<button>Click to animate</button>
<div class="box box-1">Hello!</div>
body {
  box-sizing: border-box;
  text-align: center;
}
.box {
  height: 100px;
  padding: 20px;
  text-align: center;
  color: #fff;
  opacity: 0;
}
.box-1 {
  background-color: #333;
}
$( document ).ready( function() {
  $( 'button' ).click( function() {
    $( '.box-1' ).animate( {
      opacity: '1'
    }, 2000 );
  });
});

 

연달아 나타나게 하는 방법

요소를 몇 개 더 만든 후 차례대로 나타나게 합니다.

  • 하나의 요소가 나온 후 그 다음 요소가 나오게 하기 위해서 콜백 함수를 사용합니다.
  • 시간 조절을 위해서 Delay이라는 변수를 만들었습니다.
  • 그 값을 변경하면 나타나는 속도가 바뀝니다.
<button>Click to animate</button>
<div class="box box-1">Hello!</div>
<div class="box box-2">Hello!</div>
<div class="box box-3">Hello!</div>
body {
  box-sizing: border-box;
  text-align: center;
}
.box {
  height: 100px;
  padding: 20px;
  text-align: center;
  color: #ffff;
  opacity: 0;
}
.box-1 {
  background-color: #333;
}
.box-2 {
  background-color: #222;
}
.box-3 {
  background-color: #111;
}
$( document ).ready( function() {
  $( 'button' ).click( function() {
    var Delay = 600;
    $( '.box-1' ).animate( {
      opacity: '1'
    }, Delay, function() {
      $( '.box-2' ).animate( {
        opacity: '1'
      }, Delay, function() {
        $( '.box-3' ).animate( {
          opacity: '1'
        }, Delay );
      });
    });
  });
});

You may like these posts

  • .each() .each()는 선택한 요소가 여러 개일 때 각각에 대하여 반복하여 함수를 실행시킵니다.   기본형 .each( function ) 특정 조건을 만족할 때 반복 작업에서 빠져려면 return false 를 사용합니다.   예제 1 p 요소에 각…
  • .fadeIn() .fadeIn()은 선택한 요소를 서서히 나타나게 합니다.   기본형 .fadeIn( [ duration ] [, easing ] [, complete ] )   duration duration에는 완전히 나타날 때까지의 시간이 들어갑니다. fast, sl…
  • .css() .css()로 선택한 요소의 css 속성값을 가져오거나 style 속성을 추가합니다.   기본형 1 .css( propertyName ) 속성값을 가져옵니다. 예를 들어 $( "h1" ).css( "color" ); 는 <h1> 요소의 스타일 중 color 속성의 값…
  • .delay() .delay()는 실행 중인 함수를 정해진 시간만큼 중지(연기) 시킵니다.   기본형 .delay( duration [, queueName ] ) duration에는 중지할 시간이 들어갑니다. 숫자로 정할 때의 단위는 1/1000초이고, slow 또는 fast…
  • .empty() .empty()는 선택한 요소의 내용을 지웁니다. 내용만 지울 뿐 태그는 남아있다는 것에 주의합니다. 태그를 포함한 요소 전체를 제거할 때는 .remove()를 사용합니다.   기본형 .empty() 예를 들어 <h1>Lorem</h1> 일 …
  • .detach() .detach()는 선택한 요소를 문서에서 제거합니다. 제거한다는 면에서는 .remove()와 같으나 .detach()는 제거한 요소를 저장하여 다시 사용할 수 있습니다.   기본형 .detach( [ selector ] ) 예를 들어 var abc = $( 'h1…

Post a Comment