jQuery 效果 - 动画(animate() 方法)

这篇博客详细介绍了jQuery的animate()方法,用于创建自定义动画。内容包括animate()的语法、操作多个属性、使用相对值、预定义值以及队列功能,并通过多个实例展示了如何实现各种动画效果。注意,使用animate()时需确保元素的CSS position属性已设置为relative、fixed或absolute。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jQuery 动画 - animate() 方法

jQuery animate() 方法用于创建自定义动画。

语法:

$(selector).animate({params},speed,callback);

必需的 params 参数定义形成动画的 CSS 属性。

可选的 speed 参数规定效果的时长。它可以取以下值:"slow"、"fast" 或毫秒。

可选的 callback 参数是动画完成后所执行的函数名称。

下面的例子演示 animate() 方法的简单应用;它把 <div> 元素移动到左边,直到 left 属性等于 250 像素为止:

实例

$("button").click(function(){
  $("div").animate({left:'250px'});
}); 

提示:默认地,所有 HTML 元素都有一个静态位置,且无法移动。

如需对位置进行操作,要记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute!

jQuery animate() - 操作多个属性

请注意,生成动画的过程中可同时使用多个属性:

实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
</head>

<body>
<button>开始动画</button>
<p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
<div style="background:#F00;height:100px;width:100px;position:absolute">
</div>
<script>
	$(document).ready(function(){
		$("button").click(function(){
			$("div").animate({
				left:'300px',
				opacity:'0.6',
				height:'150px',
				width:'150px'
			})
		})
	})
</script>
</body>
</html>
效果图:


jQuery animate() - 使用相对值

也可以定义相对值(该值相对于元素的当前值)。需要在值的前面加上 += 或 -=:

实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
</head>

<body>
<button>开始动画</button>
<p>默认情况下,所有 HTML 元素的位置都是静态的,并且无法移动。如需对位置进行操作,记得首先把元素的 CSS position 属性设置为 relative、fixed 或 absolute。</p>
<div style="background:#F00;height:100px;width:100px;position:absolute">
</div>
<script>
	$(document).ready(function(){
		$("button").click(function(){
			$("div").animate({
				left:'300px',
				opacity:'0.6',
				height:'+=50px',
				width:'+=50px',
			})
		})
	})
</script>
</body>
</html>

jQuery animate() - 使用预定义的值

您甚至可以把属性的动画值设置为 "show"、"hide" 或 "toggle":

实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
</head>

<body>
<button>开始动画</button>
<div style="background:#F00;height:100px;width:100px;position:absolute">
</div>
<script>
	$(document).ready(function(){
		$("button").click(function(){
			$("div").animate({
				//left:'300px',
				opacity:'0.6',
				height:'toggle',
				width:'toggle'
			})
		})
	})
</script>
</body>
</html>

jQuery animate() - 使用队列功能

默认地,jQuery 提供针对动画的队列功能。

这意味着如果您在彼此之后编写多个 animate() 调用,jQuery 会创建包含这些方法调用的“内部”队列。然后逐一运行这些 animate 调用。

实例 1

隐藏,如果您希望在彼此之后执行不同的动画,那么我们要利用队列功能:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
</head>

<body>
<button>开始动画</button>
<div style="background:#F00;height:100px;width:100px;position:absolute">
</div>

<script>

	$(document).ready(function(){
		$("button").click(function(){
			var div = $("div");
			div.animate({height:'300px',opacity:'0.4',},"slow");
			div.animate({width:'300px',opacity:'0.8',},"slow");
			div.animate({height:'100px',opacity:'0.4',},"slow");
			div.animate({width:'100px',opacity:'0.8',},"slow");
		})
	})
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值