jQuery delay() 方法详解与动画延时应用

作者:像素漫游时间:2026-10-28 13:48:15浏览:415

本文介绍 jQuery 1.4 新增的 delay() 方法,用于推迟执行队列中之后的项目。该方法支持毫秒数或 'slow'/'fast' 字符串作为 duration 参数,并可指定 queueName(默认 Fx)。通过示例展示了在标准动画队列及自定义队列中的延时效果,包括元素淡入和位置动画的延迟执行。

jQuery delay() 方法定义与参数

delay(duration,[queueName])

该方法用于设置延时,以推迟执行队列中后续的项目。此功能自 jQuery 1.4 版本引入,主要作用是为队列中的函数添加延时。它既适用于标准的动画队列,也支持自定义队列的使用场景。

duration 参数指定延时的时间长度,单位为毫秒。

queueName 参数指定队列的名称,默认为 'Fx',即标准的动画队列。

参数 描述
speed 可选。规定延迟的速度。

可能的值:

  • 毫秒
  • "slow"
  • "fast"
queueName 可选。规定队列的名称。

默认是 "fx",标准效果队列。

基础动画延时示例


$("button").click(function(){
$("#div1").delay("slow").fadeIn();
$("#div2").delay("fast").fadeIn();
}); 

完整测试代码:

多元素差异化延时演示


<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
 $("button").click(function(){
  $("#div1").delay("slow").fadeIn();
  $("#div2").delay("fast").fadeIn();
  $("#div3").delay(800).fadeIn();
  $("#div4").delay(2000).fadeIn();
  $("#div5").delay(4000).fadeIn();
 });
});
</script>
</head>

<body>
<p>This example sets different speed values for the delay() method.</p>
<button>Click to fade in boxes with a delay</button>
<br><br>
<div id="div1" style="width:90px;height:90px;display:none;background-color:black;"></div><br>
<div id="div2" style="width:90px;height:90px;display:none;background-color:green;"></div><br>
<div id="div3" style="width:90px;height:90px;display:none;background-color:blue;"></div><br>
<div id="div4" style="width:90px;height:90px;display:none;background-color:red;"></div><br>
<div id="div5" style="width:90px;height:90px;display:none;background-color:purple;"></div><br>
</body>
</html>

例:

头部与底部延迟加载动画效果

页面元素位置动画延时应用


$(document).ready(function() {
 $('#header')
 .css({ 'top':-50 })
 .delay(1000)
 .animate({'top': 0}, 800);

 $('#footer')
 .css({ 'bottom':-15 })
 .delay(1000)
 .animate({'bottom': 0}, 800); 
});

作者最新文章