5-2 云动画效果
本节编程练习不计算学习进度,请电脑登录imooc.com操作

云动画效果

云的效果与太阳的实现方式是如出一辙,但是云是有多个的,而且是要不断的重复飘动,这里介绍新的动画参数

animation-iteration-count

动画播放次数,默认值为1,infinite为无限制,假如设置为无限制,那么动画就会不停地播放

元素的布局上,我都是保留的原尺寸,如果需要自适应,单独可以用百分比控制,通过background-size 一般可以设置 100% 100%的方式平铺元素,这样设置后图片就能自适应大小缩放了

任务

打开index.html文件,在代码的53和57行填入相应代码,可以观察到小云的变化

0% {
    left: -5%;
}
100% {
    left: 100%;
}
  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='style.css' />
  8. <link rel='stylesheet' href='pageA.css' />
  9. <style type="text/css">
  10. /* 天空云 */
  11.  
  12. .cloud {
  13. z-index: 2;
  14. position: absolute;
  15. }
  16.  
  17. .cloud1 {
  18. width: 181px;
  19. height: 101px;
  20. left: -5%;
  21. top: 15%;
  22. background: url("http://img1.sycdn.imooc.com//55addfde0001aec501810101.png");
  23. }
  24.  
  25.  
  26. .cloud2 {
  27. width: 301px;
  28. height: 140px;
  29. right: -5%;
  30. background: url("http://img1.sycdn.imooc.com//55addff500016df503010140.png");
  31. }
  32.  
  33. .cloud1Anim{
  34. -webkit-animation-name: smallCloud;
  35. -webkit-animation-duration: 30s;
  36. -webkit-animation-iteration: infinite;
  37. -moz-animation-name: smallCloud;
  38. -moz-animation-duration: 30s;
  39. -moz-animation-iteration: infinite;
  40. }
  41.  
  42. .cloud2Anim{
  43. -webkit-animation-name: largeCloud;
  44. -webkit-animation-duration: 60s;
  45. -webkit-animation-iteration: infinite;
  46. -moz-animation-name: largeCloud;
  47. -moz-animation-duration: 60s;
  48. -moz-animation-iteration: infinite;
  49. }
  50.  
  51.  
  52. @-webkit-keyframes smallCloud {
  53. /*?*/
  54. }
  55.  
  56. @-moz-keyframes smallCloud {
  57. /*?*/
  58. }
  59.  
  60. @-webkit-keyframes largeCloud {
  61. 0% {
  62. right: -5%;
  63. }
  64. 100% {
  65. right: 100%;
  66. }
  67. }
  68.  
  69. @-moz-keyframes largeCloud {
  70. 0% {
  71. right: -5%;
  72. }
  73. 100% {
  74. right: 100%;
  75. }
  76. }
  77. </style>
  78. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
  79. <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.js"></script>
  80. <script type="text/javascript" src="Swipe.js"></script>
  81. <script type="text/javascript" src="BoyWalk.js"></script>
  82. </head>
  83.  
  84. <body>
  85. <div id='content'>
  86. <ul class='content-wrap'>
  87. <li>
  88. <!-- 背景 -->
  89. <div class="a_background">
  90. <div class="a_background_top"></div>
  91. <div class="a_background_middle"></div>
  92. <div class="a_background_botton"></div>
  93. </div>
  94. <!-- 云 -->
  95. <div class="cloudArea">
  96. <div class="cloud cloud1"></div>
  97. <div class="cloud cloud2"></div>
  98. </div>
  99. <!-- 太阳 -->
  100. <div id="sun"></div>
  101. </li>
  102. <li> 页面2 </li>
  103. <li> 页面3 </li>
  104. </ul>
  105. <div id="boy" class="charector"></div>
  106. </div>
  107. <div class="button">
  108. <button>开始</button>
  109. </div>
  110. </body>
  111. <script type="text/javascript">
  112. $(function() {
  113.  
  114. var container = $("#content");
  115. var swipe = Swipe(container);
  116. // 页面滚动到指定的位置
  117. function scrollTo(time, proportionX) {
  118. var distX = container.width() * proportionX;
  119. swipe.scrollTo(distX, time);
  120. }
  121.  
  122. ////////////////////////////////////////////////////////
  123. // ================== 动画处理 ====================== //
  124. ////////////////////////////////////////////////////////
  125.  
  126. //////////
  127. // 小孩走路 //
  128. //////////
  129. var boy = BoyWalk();
  130.  
  131.  
  132. // 开始
  133. $("button:first").click(function() {
  134.  
  135. // 太阳公转
  136. $("#sun").addClass('rotation');
  137.  
  138. // 飘云
  139. $(".cloud:first").addClass('cloud1Anim');
  140. $(".cloud:last").addClass('cloud2Anim');
  141.  
  142. // 开始第一次走路
  143. boy.walkTo(2000, 0.2)
  144. .then(function() {
  145. // 第一次走路完成
  146. // 开始页面滚动
  147. scrollTo(5000, 1);
  148. }).then(function() {
  149. //第二次走路
  150. return boy.walkTo(5000, 0.5);
  151. })
  152. })
  153.  
  154. })
  155. </script>
  156.  
  157. </html>
  1. /**
  2.  * 小孩走路
  3.  * @param {[type]} container [description]
  4.  */
  5. function BoyWalk() {
  6.  
  7. var container = $("#content");
  8. // 页面可视区域
  9. var visualWidth = container.width();
  10. var visualHeight = container.height();
  11.  
  12. // 获取数据
  13. var getValue = function(className) {
  14. var $elem = $('' + className + '');
  15. // 走路的路线坐标
  16. return {
  17. height: $elem.height(),
  18. top: $elem.position().top
  19. };
  20. };
  21. // 路的Y轴
  22. var pathY = function() {
  23. var data = getValue('.a_background_middle');
  24. return data.top + data.height / 2;
  25. }();
  26.  
  27. var $boy = $("#boy");
  28. var boyWidth = $boy.width();
  29. var boyHeight = $boy.height();
  30.  
  31. // 设置下高度
  32. $boy.css({
  33. top: pathY - boyHeight + 25
  34. })
  35.  
  36. // 暂停走路
  37. function pauseWalk() {
  38. $boy.addClass('pauseWalk');
  39. }
  40.  
  41. // 恢复走路
  42. function restoreWalk() {
  43. $boy.removeClass('pauseWalk');
  44. }
  45.  
  46. // css3的动作变化
  47. function slowWalk() {
  48. $boy.addClass('slowWalk');
  49. }
  50.  
  51. // 用transition做运动
  52. function stratRun(options, runTime) {
  53. var dfdPlay = $.Deferred();
  54. // 恢复走路
  55. restoreWalk();
  56. // 运动的属性
  57. $boy.transition(
  58. options,
  59. runTime,
  60. 'linear',
  61. function() {
  62. dfdPlay.resolve(); // 动画完成
  63. });
  64. return dfdPlay;
  65. }
  66.  
  67. // 开始走路
  68. function walkRun(time, dist, disY) {
  69. time = time || 3000;
  70. // 脚动作
  71. slowWalk();
  72. // 开始走路
  73. var d1 = stratRun({
  74. 'left': dist + 'px',
  75. 'top': disY ? disY : undefined
  76. }, time);
  77. return d1;
  78. }
  79.  
  80. // 计算移动距离
  81. function calculateDist(direction, proportion) {
  82. return (direction == "x" ?
  83. visualWidth : visualHeight) * proportion;
  84. }
  85.  
  86. return {
  87. // 开始走路
  88. walkTo: function(time, proportionX, proportionY) {
  89. var distX = calculateDist('x', proportionX)
  90. var distY = calculateDist('y', proportionY)
  91. return walkRun(time, distX, distY);
  92. },
  93. // 停止走路
  94. stopWalk: function() {
  95. pauseWalk();
  96. },
  97. setColoer:function(value){
  98. $boy.css('background-color',value)
  99. }
  100. }
  101. }
  1. /////////
  2. //页面滑动 //
  3. /////////
  4.  
  5.  
  6. /**
  7.  * [Swipe description]
  8.  * @param {[type]} container [页面容器节点]
  9.  * @param {[type]} options [参数]
  10.  */
  11. function Swipe(container) {
  12. // 获取第一个子节点
  13. var element = container.find(":first");
  14. var swipe = {};
  15.  
  16. // li页面数量
  17. var slides = element.find("li");
  18.  
  19. // 获取容器尺寸
  20. var width = container.width();
  21. var height = container.height();
  22.  
  23. // 设置li页面总宽度
  24. element.css({
  25. width: (slides.length * width) + 'px',
  26. height: height + 'px'
  27. });
  28.  
  29. // 设置每一个页面li的宽度
  30. $.each(slides, function(index) {
  31. var slide = slides.eq(index); // 获取到每一个li元素
  32. slide.css({
  33. width: width + 'px',
  34. height: height + 'px'
  35. });
  36. });
  37.  
  38. // 监控完成与移动
  39. swipe.scrollTo = function(x, speed) {
  40. // 执行动画移动
  41. element.css({
  42. 'transition-timing-function' : 'linear',
  43. 'transition-duration' : speed + 'ms',
  44. 'transform' : 'translate3d(-' + x + 'px,0px,0px)'
  45. });
  46. return this;
  47. };
  48.  
  49. return swipe;
  50. }
  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5.  
  6. ol,
  7. ul,
  8. li {
  9. list-style-type: none;
  10. }
  11. /*主体部分*/
  12.  
  13. #content {
  14. width: 100%;
  15. height: 100%;
  16. /* top: 20%; */
  17. overflow: hidden;
  18. position: absolute;
  19. }
  20.  
  21. .content-wrap {
  22. position: relative;
  23. }
  24.  
  25. .content-wrap > li {
  26. background: #CAE1FF;
  27. color: red;
  28. float: left;
  29. overflow: hidden;
  30. position: relative;
  31. }
  32.  
  33. li:nth-child(2) {
  34. background: #9BCD9B;
  35. }
  36.  
  37. li:nth-child(3) {
  38. background: yellow;
  39. }
  40.  
  41. a {
  42. position: absolute;
  43. top: 50%;
  44. left: 40%;
  45. }
  46.  
  47. .charector {
  48. position: absolute;
  49. left: 0%;
  50. top: 55%;
  51. position: absolute;
  52. width: 100%;
  53. height: 100%;
  54. width: 151px;
  55. height: 291px;
  56. background: url(http://img.mukewang.com/55ade248000198ae10550582.png) -0px -291px no-repeat;
  57. }
  58.  
  59. .slowWalk {
  60. -webkit-animation-name: person-slow;
  61. -webkit-animation-duration: 950ms;
  62. -webkit-animation-iteration-count: infinite;
  63. -webkit-animation-timing-function: steps(1, start);
  64. -moz-animation-name: person-slow;
  65. -moz-animation-duration: 950ms;
  66. -moz-animation-iteration-count: infinite;
  67. -moz-animation-timing-function: steps(1, start)
  68. }
  69. /*普通慢走*/
  70.  
  71. @-webkit-keyframes person-slow {
  72. 0% {
  73. background-position: -0px -291px;
  74. }
  75. 25% {
  76. background-position: -602px -0px;
  77. }
  78. 50% {
  79. background-position: -302px -291px;
  80. }
  81. 75% {
  82. background-position: -151px -291px;
  83. }
  84. 100% {
  85. background-position: -0px -291px;
  86. }
  87. }
  88.  
  89. @-moz-keyframes person-slow {
  90. 0% {
  91. background-position: -0px -291px;
  92. }
  93. 25% {
  94. background-position: -602px -0px;
  95. }
  96. 50% {
  97. background-position: -302px -291px;
  98. }
  99. 75% {
  100. background-position: -151px -291px;
  101. }
  102. 100% {
  103. background-position: -0px -291px;
  104. }
  105. }
  1. /*背景图*/
  2.  
  3. .a_background {
  4. width: 100%;
  5. height: 100%;
  6. /* background-image: url("../images/QixiA.png");
  7.   background-size: 100% 100%;*/
  8. position: absolute;
  9. }
  10.  
  11. .a_background_top {
  12. width: 100%;
  13. height: 71.6%;
  14. background-image: url("http://img1.sycdn.imooc.com//55addf6900019d8f14410645.png");
  15. background-size: 100% 100%;
  16. }
  17.  
  18. .a_background_middle {
  19. width: 100%;
  20. height: 13.1%;
  21. background-image: url("http://img1.sycdn.imooc.com//55addf800001ff2e14410118.png");
  22. background-size: 100% 100%;
  23. }
  24.  
  25. .a_background_botton {
  26. width: 100%;
  27. height: 15.3%;
  28. background-image: url("http://img1.sycdn.imooc.com//55addfcb000189b314410138.png");
  29. background-size: 100% 100%;
  30. }
  31.  
  32. button {
  33. width: 80px;
  34. height: 50px;
  35. }
  36.  
  37. .button {
  38. position: absolute;
  39. bottom: 0;
  40. }
  41.  
  42.  
  43. /*人物暂停*/
  44.  
  45. .pauseWalk {
  46. animation-play-state: paused;
  47. }
  48.  
  49.  
  50. /*-------- 太阳自转以及动画 --------*/
  51.  
  52. #sun {
  53. background: url("http://img1.sycdn.imooc.com//55ade004000106c202010201.png") no-repeat;
  54. position: absolute;
  55. z-index: 1;
  56. top: -30px;
  57. height: 201px;
  58. width: 201px;
  59. right: 40%;
  60. }
  61.  
  62. .rotation {
  63. -webkit-animation-name: rotation;
  64. -webkit-animation-duration: 30s;
  65. -webkit-animation-iteration: 1;
  66. -moz-animation-name: rotation;
  67. -moz-animation-duration: 30s;
  68. -moz-animation-iteration: 1;
  69. }
  70.  
  71. @-webkit-keyframes rotation {
  72. 0% {
  73. transform: translateX(0) translateY(0);
  74. }
  75. 100% {
  76. transform: translateX(-2000px) translateY(400px);
  77. }
  78. }
  79.  
  80. @-moz-keyframes rotation {
  81. 0% {
  82. transform: translateX(0) translateY(0);
  83. }
  84. 100% {
  85. transform: translateX(-2000px) translateY(400px);
  86. }
  87. }
下一节