3-4 运动的状态控制
本节编程练习不计算学习进度,请电脑登录imooc.com操作

运动的状态控制

通过CSS3的animation与transition的结合实现了人物的走路的效果。一般来说运动的状态都是需要可控的,这样才方便我们做进一步的操作。

animation的暂停方式

CSS3的animation直接提供一个animation-play-state的样式来控制动画的暂停处理。增加一个控制暂停样式,写动画样式的时候特别注意下不同浏览器的兼容性,加上对应的前缀

.pauseWalk {
   -webkit-animation-play-state: paused;
   -moz-animation-play-state: paused;
}

只需要在对应的有animation动画人物元素节点上,通过动态增加删除这个样式就可以控制这个精灵的开始与暂停了,非常简单

transition的暂停方式

至于transition,一般来说要暂停的地方都是一开始就程序设定好的目标点,因此这个其实是不需要控制的,这个也没办法控制,这个是一个动画过渡的效果,浏览器只提供了一个动画结束的回调。当然可以有一个变通的办法,做一个强制改变目标过渡值的处理

如何操作:

具体可以看右边的代码块,暂停方法内transition强制做了一个设置left坐标的处理,达到一个暂停的效果,但是这样是有问题的,下一次的启动必须等上一次动画的时间结束

不难看出animation要比transition强大多了

任务

打开index.html文件,在代码的90行填入相应代码,通过定义样式的方式,让精灵动画暂停

$boy.addClass('pauseWalk');
  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. button{
  11. width: 80px;
  12. height: 50px;
  13. }
  14. .button{
  15. position: absolute;
  16. bottom:0;
  17. }
  18.  
  19. /* 人物暂停 */
  20. .pauseWalk {
  21. -webkit-animation-play-state: paused;
  22. -moz-animation-play-state: paused;
  23. }
  24.  
  25. </style>
  26. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
  27. <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.js"></script>
  28. <script type="text/javascript" src="Swipe.js"></script>
  29. </head>
  30.  
  31.  
  32. <body>
  33. <div id='content'>
  34. <ul class='content-wrap'>
  35. <li>
  36. <div class="a_background">
  37. <div class="a_background_top"></div>
  38. <div class="a_background_middle"></div>
  39. <div class="a_background_botton"></div>
  40. </div>
  41. </li>
  42. <li> 页面2 </li>
  43. <li> 页面3 </li>
  44. </ul>
  45. <div id="boy" class="charector"></div>
  46. </div>
  47. <div class="button">
  48. <button>开始</button>
  49. <button>暂停</button>
  50. </div>
  51.  
  52.  
  53. <script type="text/javascript">
  54. var swipe = Swipe($("#content"));
  55. // 获取数据
  56. var getValue = function(className) {
  57. var $elem = $('' + className + '')
  58. // 走路的路线坐标
  59. return {
  60. height: $elem.height(),
  61. top: $elem.position().top
  62. };
  63. };
  64. // 路的Y轴
  65. var pathY = function() {
  66. var data = getValue('.a_background_middle');
  67. return data.top + data.height / 2;
  68. }();
  69. var $boy = $("#boy");
  70. var boyHeight = $boy.height();
  71. // 修正小男孩的正确位置
  72. $boy.css({
  73. top: pathY - boyHeight + 25
  74. });
  75.  
  76. // 开始
  77. $("button:first").click(function() {
  78. $boy.addClass('slowWalk').transition({
  79. 'left': $("#content").width() + 'px',
  80. }, 10000);
  81. });
  82.  
  83. // 暂停
  84. $("button:last").click(function() {
  85. var left = $boy.css('left');
  86. // 强制做了一个改变目标left的处理
  87. // 动画是要运行10秒,所以此时动画还是没有结束的
  88. $boy.css('left',left);
  89. // 增加暂停的样式
  90. // ?
  91. });
  92. </script>
  93. </body>
  94.  
  95. </html>
  1. /*背景图*/
  2.  
  3. .a_background {
  4. width: 100%;
  5. height: 100%;
  6. position: absolute;
  7. }
  8.  
  9. .a_background_top{
  10. width: 100%;
  11. height: 71.6%;
  12. background-image: url("http://img1.sycdn.imooc.com//55addf6900019d8f14410645.png");
  13. background-size: 100% 100%;
  14. }
  15.  
  16.  
  17. .a_background_middle{
  18. width: 100%;
  19. height: 13.1%;
  20. background-image: url("http://img1.sycdn.imooc.com//55addf800001ff2e14410118.png");
  21. background-size: 100% 100%;
  22. }
  23.  
  24. .a_background_botton{
  25. width: 100%;
  26. height: 15.3%;
  27. background-image: url("http://img1.sycdn.imooc.com//55addfcb000189b314410138.png");
  28. background-size: 100% 100%;
  29. }
  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5.  
  6. ul,
  7. li {
  8. list-style-type: none;
  9. }
  10. /*主体部分*/
  11.  
  12. #content {
  13. width: 100%;
  14. height: 100%;
  15. /* top: 20%; */
  16. overflow: hidden;
  17. position: absolute;
  18. }
  19.  
  20. .content-wrap {
  21. position: relative;
  22. }
  23.  
  24. .content-wrap > li {
  25. /* background: #CAE1FF;
  26.   color: red; */
  27. float: left;
  28. overflow: hidden;
  29. position: relative;
  30. }
  31.  
  32. /* li:nth-child(2) {
  33.   background: #9BCD9B;
  34.   }
  35.  
  36.   li:nth-child(3) {
  37.   background: yellow;
  38.   }
  39.  
  40.   a {
  41.   position: absolute;
  42.   top: 50%;
  43.   left: 40%;
  44.   } */
  45.  
  46. .charector {
  47. left: 0%;
  48. top: 55%;
  49. position: absolute;
  50. /* width: 100%;
  51.   height: 100%; */
  52. width: 151px;
  53. height: 291px;
  54. background: url(http://img.mukewang.com/55ade248000198ae10550582.png) -0px -291px no-repeat;
  55. }
  56.  
  57. .slowWalk {
  58. -webkit-animation-name: person-slow;
  59. -webkit-animation-duration: 950ms;
  60. -webkit-animation-iteration-count: infinite;
  61. -webkit-animation-timing-function: steps(1, start);
  62. -moz-animation-name: person-slow;
  63. -moz-animation-duration: 950ms;
  64. -moz-animation-iteration-count: infinite;
  65. -moz-animation-timing-function: steps(1, start)
  66. }
  67. /*普通慢走*/
  68.  
  69. @-webkit-keyframes person-slow {
  70. 0% {
  71. background-position: -0px -291px;
  72. }
  73. 25% {
  74. background-position: -602px -0px;
  75. }
  76. 50% {
  77. background-position: -302px -291px;
  78. }
  79. 75% {
  80. background-position: -151px -291px;
  81. }
  82. 100% {
  83. background-position: -0px -291px;
  84. }
  85. }
  86.  
  87. @-moz-keyframes person-slow {
  88. 0% {
  89. background-position: -0px -291px;
  90. }
  91. 25% {
  92. background-position: -602px -0px;
  93. }
  94. 50% {
  95. background-position: -302px -291px;
  96. }
  97. 75% {
  98. background-position: -151px -291px;
  99. }
  100. 100% {
  101. background-position: -0px -291px;
  102. }
  103. }
  1. /////////
  2. //页面滑动 //
  3. /////////
  4.  
  5. /**
  6.  * [Swipe description]
  7.  * @param {[type]} container [页面容器节点]
  8.  * @param {[type]} options [参数]
  9.  */
  10. function Swipe(container) {
  11. // 获取第一个子节点
  12. var element = container.find(":first");
  13. var swipe = {};
  14.  
  15. // li页面数量
  16. var slides = element.find("li");
  17.  
  18. //获取容器尺寸
  19. var width = container.width();
  20. var height = container.height();
  21.  
  22. //设置li页面总宽度
  23. element.css({
  24. width: (slides.length * width) + 'px',
  25. height: height + 'px'
  26. });
  27.  
  28. // 设置每一个页面li的宽度
  29. $.each(slides, function(index) {
  30. var slide = slides.eq(index); // 获取到每一个li元素
  31. slide.css({
  32. width: width + 'px',
  33. height: height + 'px'
  34. });
  35. });
  36.  
  37. // 监控完成与移动
  38. swipe.scrollTo = function(x, speed) {
  39. // 执行动画移动
  40. element.css({
  41. 'transition-timing-function' : 'linear',
  42. 'transition-duration' : speed + 'ms',
  43. 'transform' : 'translate3d(-' + x + 'px,0px,0px)'
  44. });
  45. return this;
  46. };
  47.  
  48. return swipe;
  49. }
下一节