λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

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

[jQuery] queue() & dequeue()

λ°˜μ‘ν˜•

[jQuery] queue() & dequeue()

1. queue(), λŒ€κΈ°μ—΄μ˜ ν•¨μˆ˜μ™€ λŒ€κΈ°μ—΄ μ‘°μž‘ν•˜κΈ°

좜처

queue([queueName]) Return : Array

  • μš”μ†Œμ— μ‹€ν–‰λ˜κ³  μžˆλŠ” ν•¨μˆ˜μ˜ 큐λ₯Ό 보여쀀닀.
    • queueName :: 큐λͺ… λ¬Έμžμ—΄, 기본값은 fx이며, κΈ°λ³Έ 효과 큐λ₯Ό μ˜λ―Έν•œλ‹€.
  • 무슨 ν•¨μˆ˜λ“€μ΄ μ‹€ν–‰λ˜κ³  μžˆλŠ”μ§€ 보여쀀닀.
<head>
  <style>
    div {
      margin: 3px; width: 40px; height: 40px; 
      position: absolute; left: 0px; top: 40px;
      background: green; display: none;
    }
    div.newcolor {
      background: blue;
    }
  </style>
</head>
<body>
  <p>The queue is length is: <span></span></p>
  <div></div>
  
  <script>
var div = $('div');

function runIt() {
  div
    .show('slow')
    .animate({left: '+=200'}, 2000)
    .slideToggle(1000)
    .slideToggle('fast')
    .animate({left: '-=200'}, 1500)
    .hide('slow')
    .show(1200)
    .slideUp('normal', runIt)
}

function showIt() {
  var n = div.queue('fx');
  $('span').text(n.length);
  setTimeout(showIt, 100);
}
runIt();
showIt();
  </script>
</body>

<span>μ—λŠ” divκ°€ μ›€μ§μ΄λŠ” 갯수λ₯Ό 보여쀀닀. ('span').text(n)만 ν•˜λ©΄ runIt()μ—μ„œ μ›€μ§μ΄λŠ” queueλ₯Ό λ³Ό 수 μžˆλ‹€.

 

$div.slideUp('normal' , runIt)은 계속 λ°˜λ³΅ν•˜λ„λ‘ 도와쀀닀.

 

queue([queueName], newQueue) Return : jQuery

  • μš”μ†Œμ— μ‹€ν–‰λ˜κ³  μžˆλŠ” ν•¨μˆ˜ 큐λ₯Ό μ‘°μž‘ν•˜κΈ°
    • queueName : Type : String / 큐λͺ… λ¬Έμžμ—΄, 기본값은 fx이며 기본효과 큐λ₯Ό 의미.
    • newQueue :: Type : Array / ν˜„μž¬ 큐λ₯Ό λŒ€μ²΄ν•  ν•¨μˆ˜ λ°°μ—΄.

queue([queueName], callback(next))

  • callback :: Type : Function( Function next() ) / 큐에 좔가될 μƒˆλ‘œμš΄ ν•¨μˆ˜. ν•¨μˆ˜κ°€ 호좜되면 큐에 μžˆλŠ” λ‹€μŒ μ•„μ΄ν…œμ΄ νμ—μ„œ 제거.

 

λͺ¨λ“  μš”μ†ŒλŠ” jQuery에 μ˜ν•΄ 자체 ν•¨μˆ˜λ“€μ„ 큐의 ν˜•νƒœλ‘œ μ§€λ‹ˆκ²Œ λœλ‹€. λŒ€λΆ€λΆ„μ˜ μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ€ 단 ν•˜λ‚˜μ˜ 큐(fx라고 ν•˜λŠ”)λ₯Ό μ‚¬μš©ν•˜κ²Œ λœλ‹€.

νλŠ” μˆœμ„œμ— μ˜ν•΄ μ•‘μ…˜ν•œλ‹€. ν•˜λ‚˜μ˜ μš”μ†Œμ— λ‹€μˆ˜μ˜ μ• λ‹ˆλ©”μ΄μ…˜ ν•¨μˆ˜λ₯Ό μ‹€ν–‰μ‹œν‚€λŠ” 방법이 일반적인 μ˜ˆμ΄λ‹€.

$('#foo').slideUp().fadeIn();

이 μ½”λ“œκ°€ μ‹€ν–‰λ˜λ©΄ μš”μ†ŒλŠ” μŠ¬λΌμ΄λ”© μ‹œμž‘ν•˜κ³  μŠ¬λΌμ΄λ”©μ΄ μ™„λ£Œλ˜λ©΄ νŽ˜μ΄λ“œ λ³€ν™”κ°€ μΌμ–΄λ‚˜κ²Œ λœλ‹€.

 

queue() ν•¨μˆ˜λŠ” μ§μ ‘μ μœΌλ‘œ ν•¨μˆ˜μ˜ λŒ€κΈ°μ—΄(큐)λ₯Ό μ‘°μž‘ν•  수 μžˆλ‹€.

 

queue()λ₯Ό μ΄μš©ν•˜μ—¬ μ½œλ°±ν•˜λŠ” 방법은 νŠΉμ΄ν•œ μ‚¬μš©λ²•μœΌλ‘œ 큐의 끝에 μƒˆλ‘œμš΄ ν•¨μˆ˜λ₯Ό μΆ”κ°€ν•΄ 넣을 수 있게 λœλ‹€.

이런 방법은 μ• λ‹ˆλ©”μ΄μ…˜ ν•¨μˆ˜λ“€μ— μ½œλ°±ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” 것과 μœ μ‚¬ν•˜λ‹€.

[예제] :: μ• λ‹ˆλ©”μ΄μ…˜ν•¨μˆ˜μ™€ queue(callback) 은 μœ μ‚¬ν•˜λ‹€.

$('#foo').slideUp();
$('#foo').queue(function() {
  alert('Animation complete.');
  $(this).dequeue();
});

μœ„ μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™λ‹€.

$('#foo').slideUp(function() {
  alert('Animation complete.');
})

$('#foo')κ°€ slideUp() 을 ν•œ 후에 alert을 좜λ ₯ν•œλ‹€. (ν•œλ²ˆλ§Œ μ‹€ν–‰)

 

.queue() 둜 ν•¨μˆ˜λ₯Ό μΆ”κ°€ν•  λ•Œ .dequeue() κ°€ κ²°κ΅­ ν˜ΈμΆœλ˜μ–΄ 라인의 λ‹€μŒ ν•¨μˆ˜κ°€ μ‹€ν–‰λ˜λ„λ‘ ν•΄μ•Όν•œλ‹€.

jQuery 1.4λΆ€ν„°λŠ” 첫번째 인자둜 λ˜λ‹€λ₯Έ ν•¨μˆ˜λ₯Ό 전달할 수 있게 λ˜μ—ˆλ‹€. 이러 μ‹μœΌλ‘œ μ‚¬μš©ν•˜λ©° μžλ™μ μœΌλ‘œ 큐에 μžˆλŠ” λ‹€μŒ ν•¨μˆ˜κ°€ νμ—μ„œ 제거되게 λœλ‹€.

$('#test').queue(function() {
  // Do some default..
  next();
});

 

[예제] :: 큐λ₯Ό μ‘°μž‘ν•΄λ³΄μž.

<head>
  <style>
    div {
      margin: 3px; width: 40px; height: 40px; 
      position: absolute; left: 0px; top: 40px;
      background: green; display: none;
    }
    div.newcolor {
      background: blue;
    }
  </style>
</head>
<body>
  Click here..
  <div></div>
  
  <script>
  $(document.body).click(function() {
  $('div')
    .show('slow')
    .aniamte({ left: '+=200'}, 2000)
    .queue(function() {
      $(this).addClass('newcolor').dequeue();
    })
    .animate({ left: '-=200'}, 500)
    .queue(function() {
      $(this).removeClas('newcolor').dequeue();
    })
    .slideUp();
  })	
  </script>
</body>

 

[예제] :: 큐 배열을 μ„ΈνŒ…ν•˜μ—¬ 큐λ₯Ό μ œκ±°ν•΄λ³΄μž.

<head>
  <style>
    div {
      margin: 3px; width: 40px; height: 40px; 
      position: absolute; left: 0px; top: 40px;
      background: green; display: none;
    }
    div.newcolor {
      background: blue;
    }
  </style>
</head>
<body>
  <button id="start">Start</button>
  <button id="stop">Stop</button>
  <div></div>
  <script>
$('#start').click(function() {
  $('div')
    .show('slow')
    .animate({ left: '+=200'}, 5000)
    .queue(function() {
      $(this).addClass('newcolor').dequeue();
    })
    .animate({ left: '-=200'}, 1500)
    .queue(function() {
      $(this).removeClass('newcolor').dequeue();
    })
    .slideUp();
});
    
$('#stop').click(function() {
    $('div').queue('fx', []).stop();
});
  </script>
</body>

queue('fx' ,[]) fxλŠ” 기본효과 큐, [] ν˜„μž¬ 큐λ₯Ό λŒ€μ²΄ν•  ν•¨μˆ˜λ°°μ—΄

stopλ²„νŠΌμ„ λˆ„λ₯΄λ©΄ κ·Έ μœ„μΉ˜μ—μ„œ μ •μ§€λ˜μ—ˆλ‹€κ°€ startλ²„νŠΌ λˆ„λ₯΄λ©΄ λͺ¨λ“  μ• λ‹ˆλ©”μ΄μ…˜μ„ μ™„λ£Œν•œλ‹€. λ‹€μ‹œ startλ²„νŠΌμ„ λˆ„λ₯΄λ©΄ μ •μ§€λ˜μ—ˆλ˜ μœ„μΉ˜μ—μ„œ λ‹€μ‹œ 움직인닀.

 


 

2. dequeue(), λŒ€κΈ°μ—΄μ˜ λ‹€μŒ ν•¨μˆ˜ μ‹€ν–‰

dequeue([queueName]) Return : jQuery

  • dequeue()ν•¨μˆ˜κ°€ 호좜되면 νμ—μ„œ λΉ μ Έλ‚˜μ™€ λ‹€μŒ ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•œλ‹€.
  • νμ—μ„œ λΉ μ Έλ‚˜μ™€ λ‹€μŒ μƒν™©μœΌλ‘œ μ§„ν–‰ν•˜κ³  싢을 λ•Œμ‚¬μš©.
  • queue() λ©”μ„œλ“œ μ‹€ν–‰ 이후에 적용된 μ• λ‹ˆλ©”μ΄μ…˜ λ©”μ„œλ“œκ°€ μ·¨μ†Œλ˜μ§€μ•Šκ²Œ μ—°κ²°ν•΄μ€€λ‹€.dequeue() κ°€ 없을 μ‹œ .animate({ left: '10px', top: '30px' }, 700); κ°€ μ‹€ν–‰λ˜μ§€ μ•ŠλŠ”λ‹€.
  • <head> <style> div { margin: 3px; width: 50px; height: 50px; position: absolute; left: 10px; top: 30px; background: yellow; } div.red { background: red; } </style> </head> <body> <button>Start</button> <div></div> <script> $('button').click(function() { $('div') .animate({ left: '+=200'}, 2000) .animate({ top: '0px'}, 600) .queue(function() { $(this).toggleClass('red').dequeue(); }) .animate({ left: '10px', top: '30px' }, 700); }) </script> </body>
λ°˜μ‘ν˜•