这篇文章将为大家详细讲解有关JavaScript如何实现自定义动画,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
自定义动画用到的几个框架函数:
$("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>
关于“JavaScript如何实现自定义动画”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://www.xuebuyuan.com/3263109.html