[jQuery知识]jQuery之知识十一-动画高级

来源:互联网 发布:java软件培训 编辑:程序博客网 时间:2024/05/20 04:50

前言

1.自定义动画 
2.列队动画方法 
3.动画相关方法 
4.动画全局属性

一.自定义动画 
jQuery 提供了几种简单常用的固定动画方面我们使用。但有些时候,这些简单动画无法 满足我们更加复杂的需求。这个时候,jQuery 提供了一个.animate()方法来创建我们的自定义动画,满足更多复杂多变的要求。

$('.animate').click(function () { $('#box').animate({'width' : '300px', 'height' : '200px', 'fontSize' : '50px',  'opacity' : 0.5}); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意:一个 CSS 变化就是一个动画效果,上面的例子中,已经有四个 CSS 变化,已经 实现了多重动画同步运动的效果。

必传的参数只有一个,就是一个键值对 CSS 变化样式的对象。还有两个可选参数分别 为速度和回调函数。

$('.animate').click(function () { $('#box').animate({'width' : '300px','height' : '200px' }, 1000, function () {alert('动画执行完毕执行我!'); });});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

到目前位置,我们都是创建的固定位置不动的动画。如果想要实现运动状态的位移动画, 那就必须使用自定义动画,并且结合 CSS 的绝对定位功能。

$('.animate').click(function () { $('#box').animate({'top' : '300px',//先必须设置 CSS 绝对定位'left' : '200px' });});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

自定义动画中,每次开始运动都必须是初始位置或初始状态,而有时我们想通过当前位 置或状态下再进行动画。jQuery 提供了自定义动画的累加、累减功能。

$('.animate').click(function () { $('#box').animate({'left' : '+=100px', });});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

自定义实现列队动画的方式,有两种:1.在回调函数中再执行一个动画;2.通过连缀或 顺序来实现列队动画。

//通过依次顺序实现列队动画$('.animate').click(function () { $('#box').animate({'left' : '100px'}); $('#box').animate({'top' : '100px'}); $('#box').animate({'width' : '300px'});});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

注意:如果不是同一个元素,就会实现同步动画

 //通过连缀实现列队动画 $('.animate').click(function () {$('#box').animate({ 'left' : '100px'}).animate({'top' : '100px'}).animate({'width' : '300px'}); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
//通过回调函数实现列队动画 $('.animate').click(function () {$('#box').animate({ 'left' : '100px'}, function () { $('#box').animate({'top' : '100px' }, function () {$('#box').animate({ 'width' : '300px'}); });}); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

二.列队动画方法

之前我们已经可以实现列队动画了,如果是同一个元素,可以依次顺序或连缀调用。如 果是不同元素,可以使用回调函数。但有时列队动画太多,回调函数的可读性大大降低。为 此,jQuery 提供了一组专门用于列队动画的方法。

//连缀无法实现按顺序列队 $('#box').slideUp('slow').slideDown('slow').css('background', 'orange');
  • 1
  • 1

注意:如果动画方法,连缀可以实依次列队,而.css()方法不是动画方法,会在一开始 传入列队之前。那么,可以采用动画方法的回调函数来解决。

//使用回调函数,强行将.css()方法排队到.slideDown()之后$('#box').slideUp('slow').slideDown('slow', function () { $(this).css('background', 'orange');});
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

但如果这样的话,当列队动画繁多的时候,可读性不但下降,而原本的动画方法不够清 晰。所以,我们的想法是每个操作都是自己独立的方法。那么 jQuery 提供了一个类似于回 调函数的方法:.queue()。

//使用.queue()方法模拟动画方法跟随动画方法之后 $('#box').slideUp('slow').slideDown('slow').queue(function () {$(this).css('background', 'orange'); });
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

现在,我们想继续在.queue()方法后面再增加一个隐藏动画,这时发现居然无法实现。 这是.queue()特性导致的。有两种方法可以解决这个问题,jQuery 的.queue()的回调函数可以 传递一个参数,这个参数是 next 函数,在结尾处调用这个 next()方法即可再连缀执行列队动 画。

//使用 next 参数来实现继续调用列队动画 $('#box').slideUp('slow').slideDown('slow').queue(function (next) {$(this).css('background', 'orange');next(); }).hide('slow');
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

因为 next 函数是 jQuery1.4 版本以后才出现的,而之前我们普遍使用的是.dequeue()方法。 意思为执行下一个元素列队中的函数。

//使用.dequeue()方法执行下一个函数动画 $('#box').slideUp('slow').slideDown('slow').queue(function () {$(this).css('background', 'orange');$(this).dequeue(); }).hide('slow');
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

如果采用顺序调用,那么使用列队动画方法,就非常清晰了,每一段代表一个列队,而 回调函数的嵌套就会杂乱无章。

//使用顺序调用的列队,逐个执行,非常清晰 $('#box').slideUp('slow'); $('#box').slideDown('slow');  $('#box').queue(function () {$(this).css('background', 'orange');$(this).dequeue(); })$('#box').hide('slow');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

.queue()方法还有一个功能,就是可以得到当前动画个列队的长度。当然,这个用法在 普通 Web 开发中用的比较少,我们这里不做详细探讨。

//获取当前列队的长度,fx 是默认列队的参数function count() {return $("#box").queue('fx').length;}//在某个动画处调用$('#box').slideDown('slow', function () {alert(count());});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

jQuery 还提供了一个清理列队的功能方法:.clearQueue()。把它放入一个列队的回调函 数或.queue()方法里,就可以把剩下为执行的列队给移除。

//清理动画列队$('#box').slideDown('slow', function () {$(this).clearQueue()});
  • 1
  • 2
  • 1
  • 2

三.动画相关方法

很多时候需要停止正在运行中的动画,jQuery 为此提供了一个.stop()方法。它有两个可 选参数:.stop(clearQueue, gotoEnd);clearQueue 传递一个布尔值,代表是否清空未执行完的 动画列队,gotoEnd 代表是否直接将正在执行的动画跳转到末状态。

//强制停止运行中的 $('.stop').click(function () {$('#box').stop(); });
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
//带参数的强制运行 $('.animate').click(function () {$('#box').animate({ 'left' : '300px'}, 1000); $('#box').animate({'bottom' : '300px' }, 1000);$('#box').animate({ 'width' : '300px'}, 1000); $('#box').animate({'height' : '300px' }, 1000);});$('.stop').click(function () { $('#box').stop(true ,true);});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

注意:第一个参数表示是否取消列队动画,默认为 false。如果参数为 true,当有列队动 画的时候,会取消后面的列队动画。第二参数表示是否到达当前动画结尾,默认为 false。 如果参数为 true,则停止后立即到达末尾处。

有时在执行动画或列队动画时,需要在运动之前有延迟执行,jQuery 为此提供了.delay() 方法。这个方法可以在动画之前设置延迟,也可以在列队动画中间加上。

//开始延迟 1 秒钟,中间延迟 1 秒 $('.animate').click(function () {$('#box').delay(1000).animate({ 'left' : '300px'}, 1000); $('#box').animate({'bottom' : '300px' }, 1000);$('#box').delay(1000).animate({ 'width' : '300px'}, 1000); $('#box').animate({'height' : '300px' }, 1000);});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

在选择器的基础章节中,我们提到过一个过滤器:animated,这个过滤器可以判断出当前 运动的动画是哪个元素。通过这个特点,我们可以避免由于用户快速在某个元素执行动画时, 由于动画积累而导致的动画和用户的行为不一致。

//递归执行自我,无线循环播放 $('#box').slideToggle('slow', function () {$(this).slideToggle('slow', arguments.callee); });//停止正在运动的动画,并且设置红色背景 $('.button').click(function(){$('div:animated').stop().css('background', 'red'); });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

四.动画全局属性

jQuery 提供了两种全局设置的属性,分别为:.fx.interval;.fx.off, 关闭页面上所有的动画。 
$.fx.interval 属性可以调整动画每秒的运行帧数,默认为 13 毫秒。数字越小越流畅,但 可能影响浏览器性能。

//设置运行帧数为 1000 毫秒 $.fx.interval = 1000;$('.button').click(function () { $('#box').toggle(3000);});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

$.fx.off 属性可以关闭所有动画效果,在非常低端的浏览器,动画可能会出现各种异常 问题导致错误。而 jQuery 设置这个属性,就是用于关闭动画效果的。

//设置动画为关闭 true$.fx.off = true; //默认为 false
  • 1
  • 2
  • 1
  • 2

补充:在.animate()方法中,还有一个参数,easing 运动方式,这个参数,大部分参数值 需要通过插件来使用,在后面的课程中,会详细讲解。自带的参数有两个:swing(缓动)、 linear(匀速),默认为 swing。

$('.button').click(function () { $('#box').animate({left : '800px' }, 'slow', 'swing');$('#pox').animate({ left : '800px' }, 'slow', 'linear');});
0 0