πŸ‘©πŸ»‍πŸ’»STUDY/jQuery

[jQuery] stop() & clearQueue() & delay()

jmjm 2020. 2. 4. 20:26

[jQuery] stop() & clearQueue() & delay()

animate stop() & Queue() & delay()

[좜처]https://webclub.tistory.com/461

1. μ• λ‹ˆλ©”μ΄μ…˜ 큐

  • νλŠ” νŠΉμ •ν•œ μ•¨λ¦¬λ¨ΌνŠΈμ—μ„œ λ°œμƒλ  μ• λ‹ˆλ©”μ΄μ…˜ λͺ©λ‘μ΄λ‹€.
  • 제이쿼리λ₯Ό μ‚¬μš©ν•΄ μ—˜λ¦¬λ¨ΌνŠΈμ—μ„œ μ• λ‹ˆλ©”μ΄μ…˜μ„ μ‹€ν–‰μ‹œν‚¬ λ•Œλ§ˆλ‹€ μ• λ‹ˆλ©”μ΄μ…˜ 큐에 μ €μž₯λœλ‹€.
  • μ—˜λ¦¬λ¨ΌνŠΈλŠ” 큐의 λͺ¨λ“  μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ™„λ£Œλ  λ•ŒκΉŒμ§€ 큐에 μ €μž₯된 μ• λ‹ˆλ©”μ΄μ…˜μ„ ν•œλ²ˆμ— ν•˜λ‚˜μ”© μ‹€ν–‰μ‹œν‚¨λ‹€.

νλŠ” λ¨Όμ € λ“€μ–΄κ°„ 것이 λ¨Όμ € λ‚˜μ˜€λŠ” 곡간을 λ§ν•œλ‹€.

jQuery의 효과 λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 효과 λͺ…령이 μ°¨λ‘€λŒ€λ‘œ 큐에듀어가고, λ“€μ–΄κ°„ μˆœμ„œλŒ€λ‘œ μ‹€ν–‰λœλ‹€.

$('.queue-box').on('click', function(){ 
	$(this)
    .aniamte({
    left : '+=60'
  }, 2000)
  .animate({
    width : '+=60'
  }, 2000)
  .animate({
    height : '+=60'
  }, 2000)
});

μœ„μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄ λ™μ‹œ(ν•œκΊΌλ²ˆ)에 μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ‹€ν–‰λ˜λŠ” 것이 μ•„λ‹ˆλΌ μ°¨λ‘€λŒ€λ‘œ μ‹€ν–‰λœλ‹€.

각 μ• λ‹ˆλ©”μ΄μ…˜μ˜ duration을 2000(2초)으둜 μ„€μ •ν–ˆμœΌλ―€λ‘œ 각 2μ΄ˆμ”© λ“±μž₯ν•˜μ—¬ 6μ΄ˆλ™μ•ˆ μ• λ‹ˆλ©”μ΄μ…˜μ΄ λ™μž‘ν•˜κ²Œ λœλ‹€.

μ• λ‹ˆλ©”μ΄μ…˜ νμ—λŠ” animate() λ©”μ„œλ“œμ˜ λ‚΄μš©μ΄ ν•˜λ‚˜ν•˜λ‚˜ λ“€μ–΄κ°€κ³  ν•˜λ‚˜ν•˜λ‚˜ λ‚˜μ˜€λ©΄μ„œ μ‹€ν–‰λ˜λ―€λ‘œ μ• λ‹ˆλ©”μ΄μ…˜μ΄ μˆœμ„œλŒ€λ‘œ λ™μž‘ν•˜κ²Œ λœλ‹€.


λ¨Όμ € λ“€μ–΄κ°„ λͺ…령을 μ°¨λ‘€λŒ€λ‘œ μ‹€ν–‰ν•˜κ²Œ λœλ‹€.

즉, λ¨Όμ € λ“€μ–΄κ°„ 것이 λ¨Όμ € λ‚˜μ˜€κ²Œ λ˜λŠ” 것이 "큐"λž€ 곡간이닀.

 

2. clearQueue()

  • clearQueue() λ©”μ†Œλ“œλŠ” 큐에 μžˆλŠ” λ‚΄μš©μ„ μ œκ±°ν•˜κ³  싢을 κ²½μš°μ— μ‚¬μš©ν•œλ‹€.
// μ• λ‹ˆλ©”μ΄μ…˜μ„ μ°¨λ‘€λŒ€λ‘œ μ‹€ν–‰ν•˜λ„λ‘ ν•œλ‹€.
$('.queue-box').animate({ left : '+=60'}, 2000);
$('.queue-box').animate({ width : '+=60'}, 2000);
$('.queue-box').animate({ height : '+=60'}, 2000);

// 3μ΄ˆν›„μ— ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•œλ‹€.
setTimeout(function(){
  // μ• λ‹ˆλ©”μ΄μ…˜μ˜ 큐λ₯Ό μ œκ±°ν•œλ‹€.
  $('.queue-box').clearQueue();
}, 3000);

clearQueue() λ©”μ†Œλ“œλŠ” 큐가 λΉ„μ›Œμ§„ μ‹œμ μ— μ• λ‹ˆλ©”μ΄μ…˜μ΄ 진행쀑인 것이 μžˆλ‹€λ©΄ ν•΄λ‹Ή μ• λ‹ˆλ©”μ΄μ…˜κΉŒμ§€λŠ” λ™μž‘ν•˜κ³  κ·Έ 이후에 μ• λ‹ˆλ©”μ΄μ…˜ 큐λ₯Ό λΉ„μš°κ²Œ λ˜λ―€λ‘œ 이후에 μΆ”κ°€λ˜λŠ” μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³ΌλŠ” λ™μž‘ν•˜μ§€ μ•Šκ²Œ λœλ‹€.

그리고 이후에 μΆ”κ°€ν•˜λŠ” νš¨κ³ΌλŠ” μ‹€ν–‰λ˜μ§€ μ•Šμ§€λ§Œ μ‹€ν–‰λ˜λ˜ μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λŠ” κΈ°λŠ₯은 μ—†λ‹€.

 

clearQueue() :: μ—¬λŸ¬κ°œμ˜ μ• λ‹ˆλ©”μ΄μ…˜μΌ 경우 ν•΄λ‹Ή μ• λ‹ˆλ©”μ΄μ…˜ λ™μž‘ν•˜κ³ , κ·Έ 이후에 μ• λ‹ˆλ©”μ΄μ…˜ 큐λ₯Ό λΉ„μš°κ²Œ λœλ‹€.
이후에 μΆ”κ°€λ˜λŠ” μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³ΌλŠ” λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€.

 

이후에 μΆ”κ°€ν•˜λŠ” νš¨κ³ΌλŠ” μ‹€ν–‰λ˜μ§€ μ•Šμ§€λ§Œ μ‹€ν–‰λ˜λ˜ μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λŠ” κΈ°λŠ₯은 μ—†λ‹€. -> stop() μ‚¬μš©

 

Q. μœ„μ˜ μ˜ˆμ œμ— clearQueue() κ°€ μ•„λ‹Œ stop()을 ν•  경우,

A. left, width, height μ• λ‹ˆλ©”μ΄μ…˜μ΄ λͺ¨λ‘ μ‹€ν–‰λœλ‹€.

3. stop()

μœ„μ—μ„œ clearQueue() λ©”μ„œλ“œλŠ” 큐λ₯Ό μ œκ±°ν•  뿐 효과 쀑간에 μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λŠ” κΈ°λŠ₯은 ν¬ν•¨ν•˜κ³  μžˆμ§€ μ•Šλ‹€.

  • μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λΌλ©΄ stop() λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.
$(selector).stop();
$(selector).stop(clrearQueue); 
$(selector).stop(clearQueue, gotoEnd);

λ§€κ°œλ³€μˆ˜ clearQueue와 gotoEndλŠ” λΆˆλ¦¬μ–Έκ°’μœΌμΈ true, falseλ₯Ό μž…λ ₯ν•œλ‹€.

κΈ°λ³Έκ°’(default) 은 stop(false, false)

첫 번째 λ§€κ°œλ³€μˆ˜μΈ clearQueueλ₯Ό true둜 μ„€μ •ν•˜λ©΄ clearQueue()λ©”μ„œλ“œλ₯Ό μ‹€ν–‰ν•˜λŠ” 것과 같은 효과λ₯Ό λ‚Έλ‹€.

두 번째 λ§€κ°œλ³€μˆ˜μΈ gotoEndλ₯Ό true둜 μ„€μ •ν•˜λ©΄ μ œμžλ¦¬μ—μ„œ λ©ˆμΆ”λŠ” 것이 μ•„λ‹Œ 효과λ₯Ό μ§€μ •ν•œ μ΅œμ’…ν˜•νƒœμ—μ„œ λ©ˆμΆ”κ²Œ λœλ‹€.

clearQueue : true :: 효과 μ• λ‹ˆλ©”μ΄μ…˜ 큐의 λ‚΄μš©μ„ 제거

goToEnd : true :: 싀행쀑인 μ• λ‹ˆλ©”μ΄μ…˜ μ΅œμ’…μœ„μΉ˜λ‘œ λ°”λ‘œ 이동

<button>stop(false, false)</button>
<button>stop(true,false)</button>
<button>stop(false, true)</button>
<button>stop(true, true)</button>
<div class="box1"></div>
$('button').on('click', function(){
	var $html = $(this).html();  
  var evalText = '$(".box1").' + $html;
  
  // λ©”μ„œλ“œμ‹€ν–‰
  eval(evalText);
});
setInterval(function(){
  $('.box1')
  	.animate({
    	left: '500'
  }, 1000)
  	.animate({
    	left: 0
  }, 1000)
}, 3000);

μœ„ νš¨κ³ΌλŠ” setInterval둜 인해 3μ΄ˆλ§ˆλ‹€ μ• λ‹ˆλ©”μ΄μ…˜μ΄ λ™μž‘ν•˜λ„λ‘ μ •μ˜λ˜μ–΄ μžˆλ‹€.

1μ΄ˆκ°„ 였λ₯Έμͺ½μœΌλ‘œ 500px을 μ΄λ™ν•˜κ³  1μ΄ˆκ°„ left: 0 으둜 인해 제자리둜 λŒμ•„μ˜€λŠ” νš¨κ³Όμ΄λ‹€. 그리고 3μ΄ˆκ°„μ˜ μΈν„°λ²Œλ‘œ 인해 2μ΄ˆκ°„μ˜ μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³Όκ°€ λλ‚œ ν›„ 1μ΄ˆκ°„ μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ •μ§€ν•œ 것과 같은 효과λ₯Ό λ‚΄κ³  μžˆλ‹€.

 

μœ„μ˜ 각 λ²„νŠΌμ„ ν΄λ¦­ν•œ νš¨κ³ΌλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

1) stop(false, false)

  • 우츑으둜 500pxμ΄λ™ν•˜λŠ” 쀑에 클릭 -> 였λ₯Έμͺ½μœΌλ‘œ μ΄λ™ν•˜λŠ” 것을 λ©ˆμΆ”κ³  μ œμžλ¦¬μ—μ„œ λ°”λ‘œ μ™Όμͺ½μœΌλ‘œ λ˜λŒμ•„κ°„λ‹€.

    stop()λ©”μ„œλ“œλŠ” 기본적으둜 μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λŠ” κΈ°λŠ₯으둜,

    우츑 이동쀑에 stop() λ²„νŠΌ ν΄λ¦­ν–ˆλ‹€λ©΄ 첫 번째 μ• λ‹ˆλ©”μ΄νŠΈ λ©”μ„œλ“œμ—μ„œ 적용된 left: 500이 λ°”λ‘œ μ •μ§€ν•˜κ³  λ‹€μŒ μ• λ‹ˆλ©”μ΄νŠΈκ°€ 적용된 left: 0의 νš¨κ³Όκ°€ λ‚˜μ™€ 제자리둜 λŒμ•„κ°€λ‚˜,

    제자리둜 λŒμ•„κ°€λŠ” 도쀑에 stop()λ²„νŠΌμ„ ν΄λ¦­ν–ˆλ‹€λ©΄ λ°”λ‘œ μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ •μ§€ν•˜κ²Œ λœλ‹€.

    μ•žμ„  μ• λ‹ˆλ©”μ΄μ…˜μΈ 우츑으둜 μ΄λ™ν•˜λŠ” νš¨κ³ΌλŠ” μ™„λ£Œλœ μƒνƒœμ΄κ³  λ§ˆμ§€λ§‰ μ• λ‹ˆλ©”μ΄μ…˜μ΄ μ‹€ν–‰ν•˜λŠ” 쀑에 stop()ν΄λ¦­ν•œ 경우라면 제자리둜 λŒμ•„μ˜€λŠ” 쀑에 λ©ˆμΆ”λŠ” 것이 λ‹Ήμ—°ν•˜λ‹€.

2) stop(true, false)

  • clearQueueκ°€ 적용된 λ²„νŠΌμœΌλ‘œ 우츑, 쒌츑으둜 μ΄λ™ν•˜λ˜ 것을 λ©ˆμΆ”κ³  setInterval()ν•¨μˆ˜κ°€ 싀행될 λ•ŒκΉŒμ§€ λŒ€κΈ°ν•œλ‹€.

    λ‹€μ‹œ 말해, μ• λ‹ˆλ©”μ΄μ…˜μ„ μ •μ§€ν•˜λ©΄μ„œ 큐λ₯Ό μ œκ±°ν•œλ‹€.

    우츑으둜 μ΄λ™μ‹œμ— λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ μ• λ‹ˆλ©”μ΄μ…˜μ΄ νš¨κ³Όκ°€ μ •μ§€ν•˜λ©΄μ„œ 두 번째 λŒ€κΈ° 쀑인 큐인 μ• λ‹ˆλ©”μ΄νŠΈ 큐가 μ œκ±°λ κ²ƒμ΄κ³ ,

    제자리둜 λŒμ•„μ˜€λŠ” 도쀑에 ν΄λ¦­ν–ˆλ‹€λ©΄ μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³Όκ°€ μ •μ§€ν•˜κ³  λŒ€κΈ°μ€‘μΈ 큐가 μ—†μœΌλ―€λ‘œ setInterval()ν•¨μˆ˜κ°€ 싀행될 λ•ŒκΉŒμ§€ λŒ€κΈ°ν•˜κ²Œ λœλ‹€.

3) stop(false, true)

  • goToEndλ₯Ό true둜 지정할 μ‹œ μ΅œμ’…μƒνƒœμ—μ„œ λ©ˆμΆ˜λ‹€.

    μš°μΈ‘λ²„νŠΌ 클릭 μ‹œ left: 500νš¨κ³Όκ°€ μ‹€ν–‰ 쀑이고 이 효과의 μ΅œμ’…μƒνƒœμ—μ„œ λ©ˆμΆ”κ²Œ λœλ‹€. κ·Έλž˜μ„œ 우츑으둜 이동쀑에 λ²„νŠΌμ„ λˆ„λ₯΄λ©΄ λΉ λ₯΄κ²Œ μˆœκ°„μ΄λ™ν•œ κ²ƒμ²˜λŸΌ 이동 ν›„ λ‹€μŒ μ• λ‹ˆλ©”μ΄νŠΈκ°€ μ‹€ν–‰ν•œλ‹€.

    그리고 쒌츑으둜 λ˜λŒμ•„μ˜€λŠ” 쀑에 λ²„νŠΌμ„ λˆŒλ €λ‹€λ©΄ left: 0의 μ΅œμ’…μƒνƒœλ‘œ λΉ λ₯΄κ²Œ νš¨κ³Όκ°€ μ™„μ„±λœ ν›„ λ©ˆμΆ˜λ‹€.

    λ‹€μ‹œ 말해, goToEnd의 true값은 ν•΄λ‹Ή μ• λ‹ˆλ©”μ΄μ…˜ 효과λ₯Ό μ΅œμ’…μƒνƒœλ‘œ μ •μ§€μ‹œν‚€λŠ” νš¨κ³Όμ΄λ‹€.

4) stop(true, true)

  • 큐λ₯Ό μ œκ±°ν•˜λŠ” λ™μ‹œμ— μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³Όλ„ μ΅œμ’…μƒνƒœλ‘œ μ •μ‹œν‚¨λ‹€.

    μš°μΈ‘λ²„νŠΌ 클릭 μ‹œ μˆœκ°„ λ°•μŠ€λŠ” 우츑 이동쀑인 효과λ₯Ό μ΅œμ’…μƒνƒœλ‘œ μ •μ§€μ‹œν‚€λ©΄μ„œ λŒ€κΈ° 쀑인 큐λ₯Ό μ œκ±°ν•œλ‹€.

    ν˜Ήμ‹œ 쒌츑으둜 λŒμ•„μ˜€κ³  있기 λ•Œλ¬Έμ— 큐가 μ œκ±°λ˜μ§€ μ•Šμ„κ±°λΌ μƒκ°ν•˜μ§€λ§Œ setInterval()λ©”μ„œλ“œ μ‹€ν–‰ ν›„ λ‹€μ‹œ 우츑으둜 μ΄λ™ν•œ ν›„ 쒌츑으둜 λŒμ•„μ˜€κΈ° λ•Œλ¬Έμ— κ·Έλ ‡κ²Œ λ³΄μ΄λŠ” 것이닀.

 


 

[μΆ”κ°€]μœ„λ³΄λ‹€ μ•„λž˜ μ˜ˆμ œκ°€ μ΄ν•΄μž˜λ¨

<div class="box"></div>
body { width: 500px; margin: 40px auto; }
.box { position: relative; width: 50px; height: 50px; background-color: #444; }
$('.box')
	.animate({left: '+=60'}, 2000)
        .aniamte({width: '+=60'}, 2000)
        .animate({height: '+=60'}, 2000)

// 3μ΄ˆν›„μ— ν•¨μˆ˜ μ‹€ν–‰.
setTimeout(function(){
  $('.box').stop(false, false); // (1)
  $('.box').stop(true, false); 	// (2)
  $('.box').stop(false, true);	// (3)
  $('.box').stop(false, false);	// (4)
}, 3000);

(1) stop(false, false)

3초 후에 stop()이 μ‹€ν–‰λ˜λ―€λ‘œ left 이동(2초) -> width 넓어지닀 λ©ˆμΆ˜λ‹€.(3초 끝!) -> height += 60 μ‹€ν–‰

(2) stop(true, false)

3초 후에 stop(true)이 μ‹€ν–‰λ˜λ―€λ‘œ left 이동(2초) -> width 넓어지닀가 λ©ˆμΆ˜λ‹€.(3초 끝!) -> 큐가 λΉ„μ›Œμ§

(3) stop(false, true)

3초 후에 stop(false, true)이 μ‹€ν–‰λ˜λ―€λ‘œ left 이동(2초) -> width 넓어지닀가(3초 끝!) animate 초(ms)λŠ” μ€‘μ§€λ˜κ³  μŠ€νƒ€μΌ μ‹€ν–‰(width += 60) -> μ΄μ–΄μ„œ height +=60 μ‹€ν–‰

(4) stop(true, true)

3초 후에 stop(true, true) 이 μ‹€ν–‰λ˜λ―€λ‘œ left 이동(2초) -> width 넓어지닀가(3초 끝) animate 초(ms) 쀑지, μŠ€νƒ€μΌ μ‹€ν–‰(width += 60)

goToEnd :: μ• λ‹ˆλ©”μ΄μ…˜ μŠ€νƒ€μΌλ§Œ 적용. msμ μš©μ•ˆλ¨.

 


 

4. delay()

  • delay() λŠ” 싀행쀑인 ν•¨μˆ˜λ₯Ό 정해진 μ‹œκ°„λ§ŒνΌ 쀑지(μ—°κΈ°)μ‹œν‚¨λ‹€.
.delay( duration, [, queueName] )

durationμ—λŠ” 쀑지할 μ‹œκ°„μ΄ λ“€μ–΄κ°„λ‹€. 숫자둜 μ •ν•  λ•ŒλŠ” λ‹¨μœ„μ˜ 1/1000초이고, slow(600) λ˜λŠ” fast(200)둜 μ •ν•  수 μžˆλ‹€.

<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<button>μ• λ‹ˆλ©”μ΄μ…˜ delay μ‹€ν–‰ν•˜κΈ°</button>
.box { margin-top: 20px; width: 50px; height: 50px; background-color: blue; position: relative; }
button { margin-top: 20px; }
$('button').on('click', function(){
  $('.box').each(function(index){
    $(this)
      .delay(index*500)
    	.animate({
      	left: '300'
    }, 'slow')
    	.delay((index+500)*500)
    	.aniamte({
      	left: 0
    }, 'slow')
  })
});

각 λ°•μŠ€λŠ” delay() λ©”μ„œλ“œλ‘œ 인해 0.5초 지연 후에 μ• λ‹ˆλ©”μ΄μ…˜ νš¨κ³Όκ°€ λ‚˜νƒ€λ‚œλ‹€.