现在的位置: 首页 > web前端 > 正文

JavaScript学习 jquery15 自定义动画

2019年05月24日 web前端 ⁄ 共 4614字 ⁄ 字号 评论关闭

自定义动画用到的几个框架函数:

$("Element").animate(params[,duration[,easing[,callback]]])
[quote]用于创建自定义动画的函数。

这个函数的关键在于指定动画形式及结果样式属性对象。这个对象中每个属性都表示一个可以变化的样式属性(如“height”、“top”或“opacity”)。 

注意:所有指定的属性必须用骆驼形式,比如用marginLeft代替margin-left,如果有不懂得骆驼命名法的朋友请看
三种通用CSS规范化命名的规则

而每个属性的值表示这个样式属性到多少时动画结束。如果是一个数值,样式属性就会从当前的值渐变到指定的值。如果使用的是“hide”、“show”或“toggle”这样的字符串值,则会为该属性调用默认的动画形式。

params
(Options) :
 一组包含作为动画属性和终值的样式属性和及其值的集合
duration (String,Number) : (可选) 三种预定速度之一的字符串("slow", "normal", or "fast")或表示动画时长的毫秒数值(如:1000)
easing (String) : (可选) 要使用的擦除效果的名称(需要插件支持).默认jQuery提供"linear" 和 "swing".
callback (Function) : (可选) 在动画完成时执行的函数




$("Element").animate(params,options)
同上
params (Options)
: 一组包含作为动画属性和终值的样式属性和及其值的集合

options (Options)
: 一组包含动画选项的值的集合

$("Element").stop()
停止指定元素上正在运行的动画


$("Element").queue()
返回指向第一个匹配元素的队列,常与length配合使用;可以将其理解为数组,一个动画数组中包含了好几个效果,queue().length表示获得当前所执行的第一个效果。


通过以上函数实现自定义动画效果:

(1)实现一个动画queue,在循环展现每个动画:

<!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" />
<title>第二十九节jQuery作业</title>
<script language="javascript"
	src="js/jquery-1.3.2.js"></script>
<script language="javascript" charset="GB2312">
	$(function() {
		$("#show").click(function() {
			var n = $("div").queue();
			$("span").text("Queue length is: " + $("div").queue().length);
		});
		runIt();
	});

	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);
	}
</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>





(2)

<!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" />
<title>第二十九节jQuery作业</title>
<script language="javascript"
	src="js/jquery-1.3.2.js"></script>
<script language="javascript" charset="GB2312">
	$(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>

(3)

<!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" />
<title>第二十九节jQuery作业</title>
<script language="javascript"
	src="js/jquery-1.3.2.js"></script>
<script language="javascript" charset="GB2312">
	$(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>

(4)

<!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" />
<title>第二十九节jQuery作业</title>
<script language="javascript"
	src="js/jquery-1.3.2.js"></script>
<script language="javascript" charset="GB2312">
	$(function() {
		$("button").click(function() {
			$("div").animate({
				left : '+=200px'
			}, 2000);
			$("div").animate({
				top : '0px'
			}, 600);
			$("div").queue(function() {
				$(this).toggleClass("red");
				$(this).dequeue();
			});
			$("div").animate({
				left : '10px',
				top : '30px'
			}, 700);
		});
	});
</script>
<style>
div {
	margin: 3px;
	width: 50px;
	position: absolute;
	height: 50px;
	left: 10px;
	top: 30px;
	background-color: yellow;
}

div.red {
	background-color: red;
}
</style>
</head>
<body>
	<button>Start</button>
	<div></div>
</body>
</html>

抱歉!评论已关闭.