当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


JQuery .queue()用法及代码示例

显示或操作要在匹配元素上执行的函数队列。

用法一

.queue(  [queueName ] ) => Array

说明:显示要在匹配元素上执行的函数队列。

  • 添加的版本:1.2.queue( [queueName ] )

    • queueName
      类型:String
      包含队列名称的字符串。默认为 fx ,标准效果队列。

例子:

显示队列的长度。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>queue demo</title>
  <style>
  div {
    margin: 3px;
    width: 40px;
    height: 40px;
    position: absolute;
    left: 0px;
    top: 60px;
    background: green;
    display: none;
  }
  div.newcolor {
    background: blue;
  }
  p {
    color: red;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<p>The queue 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>
</html>

演示:

用法二

.queue(  [queueName ], newQueue ) => jQuery

说明:操作要执行的函数队列,对每个匹配的元素执行一次。

  • 添加的版本:1.2.queue( [queueName ], newQueue )

    • queueName
      类型:String
      包含队列名称的字符串。默认为 fx ,标准效果队列。
    • newQueue
      类型:Array
      用于替换当前队列内容的函数数组。
  • 添加的版本:1.2.queue( [queueName ], callback )

    • queueName
      类型:String
      包含队列名称的字符串。默认为 fx ,标准效果队列。
    • callback
      类型:Function(Functionnext())
      添加到队列中的新函数,调用的函数将使下一个项目出列。

每个元素都可以通过 jQuery 附加一对多的函数队列。在大多数应用程序中,只使用一个队列(称为 fx )。队列允许在元素上异步调用一系列操作,而不会停止程序执行。典型的例子是在一个元素上调用多个动画方法。例如:

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

执行此语句时,元素立即开始其滑动动画,但淡入淡出的过渡被放置在fx 队列中,只有在滑动过渡完成后才被调用。

.queue() 方法允许我们直接操作这个函数队列。使用回调调用.queue() 特别有用;它允许我们在队列末尾放置一个新函数。回调函数对 jQuery 集中的每个元素执行一次。

此函数类似于提供带有动画方法的回调函数,但不需要在执行动画时给出回调。

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

这相当于:

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

请注意,当使用 .queue() 添加函数时,我们应确保最终调用 .dequeue() 以便执行行中的下一个函数。

从 jQuery 1.4 开始,被调用的函数作为第一个参数传递给另一个函数。调用时,它会自动使下一个项目出列并保持队列移动。我们使用它如下:

$( "#test" ).queue(function( next ) {
    // Do some stuff...
    next();
});

例子:

排队一个自定义函数。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>queue demo</title>
  <style>
  div {
    margin: 3px;
    width: 40px;
    height: 40px;
    position: absolute;
    left: 0px;
    top: 30px;
    background: green;
    display: none;
  }
  div.newcolor {
    background: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
Click here...
<div></div>
 
<script>
$( document.body ).click(function() {
  $( "div" )
    .show( "slow" )
    .animate({ left: "+=200" }, 2000 )
    .queue(function() {
      $( this ).addClass( "newcolor" ).dequeue();
    })
    .animate({ left: "-=200" }, 500 )
    .queue(function() {
      $( this ).removeClass( "newcolor" ).dequeue();
    })
    .slideUp();
});
</script>
 
</body>
</html>

演示:

设置一个队列数组来删除队列。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>queue demo</title>
  <style>
  div {
    margin: 3px;
    width: 40px;
    height: 40px;
    position: absolute;
    left: 0px;
    top: 30px;
    background: green;
    display: none;
  }
  div.newcolor {
    background: blue;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</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>
</html>

演示:

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 .queue()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。