5-2 圣诞雪橇
本节编程练习不计算学习进度,请电脑登录imooc.com操作

圣诞雪橇

在第一副页面中,雪橇驮着小男孩从远方渐渐的飞到小屋窗前,雪橇运动变化就是关键帧的精灵动画与transition的结合的效果

参考右边代码:christmas.js中调用new pageA场景类。pageA.js中的场景类pageA,定义了next、run与stopWalk方法,都是用来控制雪橇动画的

雪橇的逻辑:

  1. 首先雪橇图是一组合成的雪碧图,在pageA.css中定义的类.chs-boy, 通过设置background-size的手段让其自适应分辨率
  2. 雪橇图是由小变大的一个过程,所以在初始的时候应该赋予一个scale(0.1)的缩放值,为什么不值得采用0,因为用的插件会对0直接做处理
  3. pageA.css中,定义一组关键帧样式chs-boy-deer,让雪橇精灵图产生一组动画效果,4张图轮流播放
  4. pageA.js中,通过run方法,调用transition方法,让雪橇产生right与top坐标值的变化
  5. 雪橇有正反2面的效果,可以通过rotateY(-180deg)切换

值得注意的是:在代码的编写上,我合并了next方法的处理,增加了Deferred异步的方法,让异步的代码可以有同步执行的逻辑,这样代码结构看起来会更清晰

在next方法中封装了Deferred是使用,所以可以连续调用

next().then().then().then().......

具体可以参考3-6异步编程那一节,通过jQuery的Deferred对象改造异步流程

任务

在pageA.js中64行处填入任务代码

参考next方法,填充雪橇运行的最后一段距离

在7000毫秒的时间内,继续飞行到坐标top = 7.8rem,right=1.2rem的位置

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6. <title>圣诞主题</title>
  7. <link rel='stylesheet' href='common.css' />
  8. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
  9. <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.js"></script>
  10. <link rel="stylesheet" type="text/css" href="pageA.css">
  11. <script src="pageA.js"></script>
  12. <script src="christmas.js"></script>
  13. </head>
  14.  
  15. <body>
  16. <section class="container">
  17. <!-- 第一幅画面 -->
  18. <section class="page-a bg-adaptive">
  19. <!-- 男孩 -->
  20. <div class="chs-boy chs-boy-deer"></div>
  21. </section>
  22. <!-- 第二幅画面 -->
  23. <section class="page-b bg-adaptive">
  24. </section>
  25. <!-- 第三幅画面 -->
  26. <section class="page-c bg-adaptive">
  27. </section>
  28. </section>
  29. <button>点击运行雪橇动作</button>
  30. <script type="text/javascript">
  31. //rem设置
  32. (function(doc, win) {
  33. var docEl = doc.documentElement,
  34. resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
  35. recalc = function() {
  36. var clientWidth = docEl.clientWidth;
  37. if (!clientWidth) return;
  38. docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
  39. //宽与高度
  40. document.body.style.height = clientWidth * (900 / 1440) + "px"
  41. };
  42. win.addEventListener(resizeEvt, recalc, false);
  43. doc.addEventListener('DOMContentLoaded', recalc, false);
  44. })(document, window);
  45.  
  46. </script>
  47. </body>
  48.  
  49. </html>
  50.  
  1. /**
  2.  * 慕课网特制
  3.  * 圣诞主题效果
  4.  * @type {Object}
  5.  */
  6.  
  7. /**
  8.  * 中间调用
  9.  */
  10. var Christmas = function() {
  11. //页面容器元素
  12. var $pageA = $(".page-a");
  13.  
  14. //构建第一个场景页面对象
  15. new pageA($pageA);
  16. };
  17.  
  18. $(function() {
  19. $("button").click(function(){
  20. //圣诞主题效果,开始
  21. Christmas()
  22. })
  23. })
  1. /**
  2.  * 第一副场景页面
  3.  * @param {[type]} argument [description]
  4.  * @return {[type]} [description]
  5.  */
  6. function pageA (element) {
  7. //根元素
  8. this.$root = element;
  9. //小男孩
  10. this.$boy = element.find(".chs-boy");
  11. //运行动画
  12. this.run();
  13. }
  14.  
  15. /**
  16.  * 运行下一个动画
  17.  * @return {Function} [description]
  18.  */
  19. pageA.prototype.next = function(options) {
  20. var dfd = $.Deferred();
  21. this.$boy.transition(options.style, options.time, "linear",function() {
  22. dfd.resolve()
  23. });
  24. return dfd;
  25. }
  26.  
  27.  
  28. /**
  29.  * 停止走路
  30.  * @return {[type]} [description]
  31.  */
  32. pageA.prototype.stopWalk = function(){
  33. this.$boy.removeClass("chs-boy-deer")
  34. }
  35.  
  36.  
  37. /**
  38.  * 路径
  39.  * @return {[type]} [description]
  40.  */
  41. pageA.prototype.run = function(callback){
  42. var that = this;
  43. var next = function() {
  44. return this.next.apply(this, arguments)
  45. }.bind(this)
  46.  
  47. next({
  48. "time": 10000,
  49. "style": {
  50. "top": "4rem",
  51. "right": "16rem",
  52. "scale": "1.2"
  53. }
  54. })
  55. .then(function() {
  56. return next({
  57. "time":500,
  58. "style": {
  59. "rotateY" : "-180",
  60. "scale": "1.5"
  61. }
  62. })
  63. })
  64. //?
  65. .then(function(){
  66. that.stopWalk();
  67. })
  68.  
  69. }
  70.  
  71.  
  72.  
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5.  
  6.  
  7. .container {
  8. width: 100%;
  9. height: 100%;
  10. position: relative;
  11. overflow: hidden;
  12. }
  13.  
  14. .bg-adaptive {
  15. background-size: 100% 100%;
  16. }
  17.  
  1. /**
  2.  * 背景布置
  3.  */
  4. .container .page-a {
  5. width : 100%;
  6. height : 100%;
  7. background-image: url("http://img1.sycdn.imooc.com//565d07770001790814410901.png");
  8. position: absolute;
  9. z-index: 5;
  10. }
  11.  
  12.  
  13. /**
  14.  * 圣诞男孩
  15.  */
  16.  
  17. .chs-boy {
  18. width : 5rem;
  19. height : 1.5rem;
  20. position : absolute;
  21. z-index : 3;
  22. top : .1rem;
  23. right : -3rem;
  24. transform : scale(0.1);
  25. background : url(http://img.mukewang.com/565d07490001365329660269.png) -300% -100%;
  26. background-size : 400% 100%;
  27. }
  28.  
  29. /**
  30.  * 男孩走路动作
  31.  */
  32.  
  33. .chs-boy-deer {
  34. -webkit-animation:chsBoyDeer 0.75s steps(3,end) infinite;
  35. -moz-animation:chsBoyDeer 0.75s steps(3,end) infinite;
  36. }
  37.  
  38. @-webkit-keyframes chsBoyDeer {
  39. 0% {
  40. background-position: -0% 100%;
  41. }
  42. 100% {
  43. background-position: -300% 100%;
  44. }
  45. }
  46. @-moz-keyframes chsBoyDeer {
  47. 0% {
  48. background-position: -0% 100%;
  49. }
  50. 100% {
  51. background-position: -300% 100%;
  52. }
  53. }
  54.  
  55.  
  56. /**
  57.  * 人物停止
  58.  */
  59.  
  60. .boy-stop-animate {
  61. -webkit-animation-play-state: paused;
  62. }
  63.  
下一节