ארגומנטים
.queue( [ queueName ] )
fx,
תור של אפקטים סטנדרטים.
דוגמה
מציג אורך של התור.
$("#show").click(function () {
var n = $("div").queue("fx");
$("span").text("Queue length is: " + n.length);
});
function runIt() {
$("div").show("slow");
$("div").animate({left:'+=200'},2000);
$("div").slideToggle(1000);
$("div").slideToggle("fast");
$("div").animate({left:'-=200'},1500);
$("div").hide("slow");
$("div").show(1200);
$("div").slideUp("normal", runIt);
}
runIt();
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $("#show").click(function () { var n = $("div").queue("fx"); $("span").text("Queue length is: " + n.length); }); function runIt() { $("div").show("slow"); $("div").animate({left:'+=200'},2000); $("div").slideToggle(1000); $("div").slideToggle("fast"); $("div").animate({left:'-=200'},1500); $("div").hide("slow"); $("div").show(1200); $("div").slideUp("normal", runIt); } runIt(); }); </script> <style> div { margin:3px; width:40px; height:40px; position:absolute; left:0px; top:30px; background:green; display:none; } div.newcolor { background:blue; } span { color:red; } </style> </head> <body> <button id="show">Show Length of Queue</button> <span></span> <div></div> </body> </html>
ארגומנטים
.queue( [ queueName ], newQueue )
fx,
אפקטים של תור סטנדרטיים.
.queue( [ queueName ], callback( next ) )
fx,
אפקטים של תור סטנדרטיים.
כל אלמנט יכול להכיל מאחד עד מספר רב של פונקציות המצורפות אליו באמצעות
jQuery.
ביישומים רבים, רק תור אחד
(
שנקרא fx
)
בשימוש.
תור מאפשר לנו להגדיר רצף פעולות,
שתופעל על ידי אלמנט אסינכרוני, ללא הצירת ביצוע תוכנה.
דוגמה טיפוסית של הפעלה הזו היא קריא למספר שיטות אנימציה עבור אלמנט.
לדוגמה:
כאשר פונרציה הזו מתבצעת, אלמנט מתחיל מייד אנימציה גלישה,
אבל המעבר דהייה נמצא בתור
fx
ויתבצע פעם אחד כשה אנימציה גלישה תסתיים.
שיטה .queue()
מאפשרת לנו לבצע מניפולציות על תור של פונקציות.
הפעלת .queue()
יחד עם קריא חוזרת מאוד שימושית;
היא מאפשרת לנו לשים פונקציה חדשה בסוף התור.
פונקציה הזו דומה למתן פונקציה חוזרת עם שיטת אנימציה, אבל לא דורשת קריא חוזרת, כדי תינתן בזמן האנימציה מתבצעת.
זה שווה ל:
$('#foo').slideUp(function() {
alert('Animation complete.');
});
שימו לב, שכאשר פונקציה מתווספת עם
.queue(), אנחנו בטוחים, ש
.dequeue()
תתבצע בהתאם בסוף.
ב- jQuery 1.4 פונקציה, שמופעלת באמצעות העברה לפונקציה אחרת כארגומנט ראשון, כשה נקרא מעביר תור אוטומטי לסאיף הבא. אתם יכולים להשתמש בדרך הבאה:
$("#test").queue(function(next) {
// Do some stuff...
next();
});
דוגמאות
דוגמה 1
שם בתור פונקצית משתמש.
$(document.body).click(function () {
$("div").show("slow");
$("div").animate({left:'+=200'},2000);
$("div").queue(function () {
$(this).addClass("newcolor");
$(this).dequeue();
});
$("div").animate({left:'-=200'},500);
$("div").queue(function () {
$(this).removeClass("newcolor");
$(this).dequeue();
});
$("div").slideUp();
});
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $(document.body).click(function () { $("div").show("slow"); $("div").animate({left:'+=200'},2000); $("div").queue(function () { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({left:'-=200'},500); $("div").queue(function () { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }); }); </script> <style> div { margin:3px; width:40px; height:40px; position:absolute; left:0px; top:30px; background:green; display:none; } div.newcolor { background:blue; } </style> </head> <body> Click here... <div></div> </body> </html>
דוגמה 2
מגדיר מערך למחיקת תור.
$("#start").click(function () {
$("div").show("slow");
$("div").animate({left:'+=200'},5000);
$("div").queue(function () {
$(this).addClass("newcolor");
$(this).dequeue();
});
$("div").animate({left:'-=200'},1500);
$("div").queue(function () {
$(this).removeClass("newcolor");
$(this).dequeue();
});
$("div").slideUp();
});
$("#stop").click(function () {
$("div").queue("fx", []);
$("div").stop();
});
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $("#start").click(function () { $("div").show("slow"); $("div").animate({left:'+=200'},5000); $("div").queue(function () { $(this).addClass("newcolor"); $(this).dequeue(); }); $("div").animate({left:'-=200'},1500); $("div").queue(function () { $(this).removeClass("newcolor"); $(this).dequeue(); }); $("div").slideUp(); }); $("#stop").click(function () { $("div").queue("fx", []); $("div").stop(); }); }); </script> <style> div { margin:3px; width:40px; height:40px; position:absolute; left:0px; top:30px; background:green; display:none; } div.newcolor { background:blue; } </style> </head> <body> <button id="start">Start</button> <button id="stop">Stop</button> <div></div> </body> </html>

