我发现queue()/ 上的jQuery.com文档dequeue()太简单了.jQuery中的队列究竟是什么?我应该如何使用它们?
gna*_*arf 489
.queue()和的用法.dequeue()jQuery中的队列用于动画.您可以将它们用于任何您喜欢的目的.它们是使用每个元素存储的函数数组jQuery.data().它们是先进先出(FIFO).您可以通过调用向队列添加一个函数.queue(),然后使用删除(通过调用)函数.dequeue().
要理解内部jQuery队列函数,阅读源代码并查看示例可以极大地帮助我.我见过的队列函数的最好例子之一是.delay():
$.fn.delay = function( time, type ) {
time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
type = type || "fx";
return this.queue( type, function() {
var elem = this;
setTimeout(function() {
jQuery.dequeue( elem, type );
}, time );
});
};
Run Code Online (Sandbox Code Playgroud)
fxjQuery中的默认队列是fx.默认队列具有一些不与其他队列共享的特殊属性.
$(elem).queue(function(){});的fx队列自动将dequeue下一个功能,并运行它,如果队列尚未开始.dequeue()从fx队列中得到一个函数时,它将unshift()(推入数组的第一个位置)字符串"inprogress"- 它标记当前正在运行的队列.fx队列.animate()由默认情况下调用它的所有函数使用.注意:如果您使用的是自定义队列,则必须手动执行.dequeue()这些功能,它们不会自动启动!
您可以通过.queue()不带函数参数调用来检索对jQuery队列的引用.如果要查看队列中有多少项,可以使用该方法.您可以使用push,pop,unshift,shift操纵队列到位.您可以通过将数组传递给.queue()函数来替换整个队列.
快速示例:
// lets assume $elem is a jQuery object that points to some element we are animating.
var queue = $elem.queue();
// remove the last function from the animation queue.
var lastFunc = queue.pop();
// insert it at the beginning:
queue.unshift(lastFunc);
// replace queue with the first three items in the queue
$elem.queue(queue.slice(0,3));
Run Code Online (Sandbox Code Playgroud)
fx)队列示例:$(function() {
// lets do something with google maps:
var $map = $("#map_canvas");
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {zoom: 8, center: myLatlng, mapTypeId: google.maps.MapTypeId.ROADMAP};
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map($map[0], myOptions);
var resized = function() {
// simple animation callback - let maps know we resized
google.maps.event.trigger(map, 'resize');
};
// wait 2 seconds
$map.delay(2000);
// resize the div:
$map.animate({
width: 250,
height: 250,
marginLeft: 250,
marginTop:250
}, resized);
// geocode something
$map.queue(function(next) {
// find stackoverflow's whois address:
geocoder.geocode({'address': '55 Broadway New York NY 10006'},handleResponse);
function handleResponse(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var location = results[0].geometry.location;
map.setZoom(13);
map.setCenter(location);
new google.maps.Marker({ map: map, position: location });
}
// geocoder result returned, continue with animations:
next();
}
});
// after we find stack overflow, wait 3 more seconds
$map.delay(3000);
// and resize the map again
$map.animate({
width: 500,
height: 500,
marginLeft:0,
marginTop: 0
}, resized);
});
Run Code Online (Sandbox Code Playgroud)
var theQueue = $({}); // jQuery on an empty object - a perfect queue holder
$.each([1,2,3],function(i, num) {
// lets add some really simple functions to a queue:
theQueue.queue('alerts', function(next) {
// show something, and if they hit "yes", run the next function.
if (confirm('index:'+i+' = '+num+'\nRun the next function?')) {
next();
}
});
});
// create a button to run the queue:
$("<button>", {
text: 'Run Queue',
click: function() {
theQueue.dequeue('alerts');
}
}).appendTo('body');
// create a button to show the length:
$("<button>", {
text: 'Show Length',
click: function() {
alert(theQueue.queue('alerts').length);
}
}).appendTo('body');
Run Code Online (Sandbox Code Playgroud)
我开发了一个$.ajaxQueue()使用了插件$.Deferred,.queue()和$.ajax()也传回一个承诺的请求完成时得到解决.另一个版本$.ajaxQueue仍然适用于1.4 版本发布在我对Sequencing Ajax Requests的回答中
/*
* jQuery.ajaxQueue - A queue for ajax requests
*
* (c) 2011 Corey Frang
* Dual licensed under the MIT and GPL licenses.
*
* Requires jQuery 1.5+
*/
(function($) {
// jQuery on an empty object, we are going to use this as our Queue
var ajaxQueue = $({});
$.ajaxQueue = function( ajaxOpts ) {
var jqXHR,
dfd = $.Deferred(),
promise = dfd.promise();
// queue our ajax request
ajaxQueue.queue( doRequest );
// add the abort method
promise.abort = function( statusText ) {
// proxy abort to the jqXHR if it is active
if ( jqXHR ) {
return jqXHR.abort( statusText );
}
// if there wasn't already a jqXHR we need to remove from queue
var queue = ajaxQueue.queue(),
index = $.inArray( doRequest, queue );
if ( index > -1 ) {
queue.splice( index, 1 );
}
// and then reject the deferred
dfd.rejectWith( ajaxOpts.context || ajaxOpts,
[ promise, statusText, "" ] );
return promise;
};
// run the actual query
function doRequest( next ) {
jqXHR = $.ajax( ajaxOpts )
.done( dfd.resolve )
.fail( dfd.reject )
.then( next, next );
}
return promise;
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
我现在已将此作为一篇关于learn.jquery.com的文章添加,该网站上有关于队列的其他精彩文章,请看看.
Sol*_*ogi 42
要理解队列方法,您必须了解jQuery如何进行动画制作.如果你一个接一个地编写多个animate方法调用,jQuery会创建一个"内部"队列并将这些方法调用添加到它.然后它逐个运行那些动画调用.
考虑以下代码.
function nonStopAnimation()
{
//These multiple animate calls are queued to run one after
//the other by jQuery.
//This is the reason that nonStopAnimation method will return immeidately
//after queuing these calls.
$('#box').animate({ left: '+=500'}, 4000);
$('#box').animate({ top: '+=500'}, 4000);
$('#box').animate({ left: '-=500'}, 4000);
//By calling the same function at the end of last animation, we can
//create non stop animation.
$('#box').animate({ top: '-=500'}, 4000 , nonStopAnimation);
}
Run Code Online (Sandbox Code Playgroud)
'queue'/'dequeue'方法可让您控制此'动画队列'.
默认情况下,动画队列名为"fx".我在这里创建了一个示例页面,其中包含各种示例,这些示例将说明如何使用队列方法.
http://jsbin.com/zoluge/1/edit?html,output
以上示例页面的代码:
$(document).ready(function() {
$('#nonStopAnimation').click(nonStopAnimation);
$('#stopAnimationQueue').click(function() {
//By default all animation for particular 'selector'
//are queued in queue named 'fx'.
//By clearning that queue, you can stop the animation.
$('#box').queue('fx', []);
});
$('#addAnimation').click(function() {
$('#box').queue(function() {
$(this).animate({ height : '-=25'}, 2000);
//De-queue our newly queued function so that queues
//can keep running.
$(this).dequeue();
});
});
$('#stopAnimation').click(function() {
$('#box').stop();
});
setInterval(function() {
$('#currentQueueLength').html(
'Current Animation Queue Length for #box ' +
$('#box').queue('fx').length
);
}, 2000);
});
function nonStopAnimation()
{
//These multiple animate calls are queued to run one after
//the other by jQuery.
$('#box').animate({ left: '+=500'}, 4000);
$('#box').animate({ top: '+=500'}, 4000);
$('#box').animate({ left: '-=500'}, 4000);
$('#box').animate({ top: '-=500'}, 4000, nonStopAnimation);
}
Run Code Online (Sandbox Code Playgroud)
现在您可能会问,我为什么要打扰这个队列?通常,你不会.但是如果你有一个想要控制的复杂动画序列,那么队列/出队方法就是你的朋友.
另请参阅jQuery小组关于创建复杂动画序列的有趣对话.
演示动画:
http://www.exfer.net/test/jquery/tabslide/
如果您还有疑问,请告诉我.
enf*_*644 20
以下是队列中多个对象动画的简单示例.
Jquery让我们只对一个对象进行排队.但在动画功能中我们可以访问其他对象.在这个例子中,我们在#qs对象上构建队列,同时为#box1和#box2对象设置动画.
将队列视为一组函数.因此,您可以将队列操作为数组.您可以使用push,pop,unshift,shift来操作队列.在此示例中,我们从动画队列中删除最后一个函数,并将其插入到开头.
完成后,我们通过dequeue()函数启动动画队列.
HTML:
<button id="show">Start Animation Queue</button>
<p></p>
<div id="box1"></div>
<div id="box2"></div>
<div id="q"></div>
Run Code Online (Sandbox Code Playgroud)
JS:
$(function(){
$('#q').queue('chain',function(next){
$("#box2").show("slow", next);
});
$('#q').queue('chain',function(next){
$('#box1').animate(
{left: 60}, {duration:1000, queue:false, complete: next}
)
});
$('#q').queue('chain',function(next){
$("#box1").animate({top:'200'},1500, next);
});
$('#q').queue('chain',function(next){
$("#box2").animate({top:'200'},1500, next);
});
$('#q').queue('chain',function(next){
$("#box2").animate({left:'200'},1500, next);
});
//notice that show effect comes last
$('#q').queue('chain',function(next){
$("#box1").show("slow", next);
});
});
$("#show").click(function () {
$("p").text("Queue length is: " + $('#q').queue("chain").length);
// remove the last function from the animation queue.
var lastFunc = $('#q').queue("chain").pop();
// insert it at the beginning:
$('#q').queue("chain").unshift(lastFunc);
//start animation queue
$('#q').dequeue('chain');
});
Run Code Online (Sandbox Code Playgroud)
CSS:
#box1 { margin:3px; width:40px; height:40px;
position:absolute; left:10px; top:60px;
background:green; display: none; }
#box2 { margin:3px; width:40px; height:40px;
position:absolute; left:100px; top:60px;
background:red; display: none; }
p { color:red; }
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 15
它允许您排队动画...例如,而不是这个
$('#my-element').animate( { opacity: 0.2, width: '100px' }, 2000);
Run Code Online (Sandbox Code Playgroud)
其衰要素,将宽度100像素在同一时间.使用队列可以暂存动画.所以一个接一个完成.
$("#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();
Run Code Online (Sandbox Code Playgroud)
来自http://docs.jquery.com/Effects/queue的示例
小智 8
这个帖子对我的问题很有帮助,但是我以不同的方式使用了$ .queue,并认为我会在这里发布我想出的内容.我需要的是一系列要触发的事件(帧),但是要动态构建的序列.我有一个可变数量的占位符,每个占位符都应包含一个动画序列的图像.数据保存在一个数组数组中,因此我循环遍历数组,为每个占位符构建每个序列,如下所示:
/* create an empty queue */
var theQueue = $({});
/* loop through the data array */
for (var i = 0; i < ph.length; i++) {
for (var l = 0; l < ph[i].length; l++) {
/* create a function which swaps an image, and calls the next function in the queue */
theQueue.queue("anim", new Function("cb", "$('ph_"+i+"' img').attr('src', '/images/"+i+"/"+l+".png');cb();"));
/* set the animation speed */
theQueue.delay(200,'anim');
}
}
/* start the animation */
theQueue.dequeue('anim');
Run Code Online (Sandbox Code Playgroud)
这是我已经得到的脚本的简化版本,但是应该显示原理 - 当一个函数被添加到队列时,它是使用Function构造函数添加的 - 这样函数可以使用循环中的变量动态编写( S).注意函数传递next()调用的参数的方式,并在最后调用它.在这种情况下,函数没有时间依赖性(它不使用$ .fadeIn或类似的东西),所以我使用$ .delay错开帧.
| 归档时间: |
|
| 查看次数: |
109844 次 |
| 最近记录: |