通过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');
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>慕课七夕主题</title> <link rel='stylesheet' href='style.css' /> <link rel='stylesheet' href='pageA.css' /> <style type="text/css"> button{ width: 80px; height: 50px; } .button{ position: absolute; bottom:0; } /* 人物暂停 */ .pauseWalk { -webkit-animation-play-state: paused; -moz-animation-play-state: paused; } </style> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.js"></script> <script type="text/javascript" src="Swipe.js"></script> </head> <body> <div id='content'> <ul class='content-wrap'> <li> <div class="a_background"> <div class="a_background_top"></div> <div class="a_background_middle"></div> <div class="a_background_botton"></div> </div> </li> <li> 页面2 </li> <li> 页面3 </li> </ul> <div id="boy" class="charector"></div> </div> <div class="button"> <button>开始</button> <button>暂停</button> </div> <script type="text/javascript"> var swipe = Swipe($("#content")); // 获取数据 var getValue = function(className) { var $elem = $('' + className + '') // 走路的路线坐标 return { height: $elem.height(), top: $elem.position().top }; }; // 路的Y轴 var pathY = function() { var data = getValue('.a_background_middle'); return data.top + data.height / 2; }(); var $boy = $("#boy"); var boyHeight = $boy.height(); // 修正小男孩的正确位置 $boy.css({ top: pathY - boyHeight + 25 }); // 开始 $("button:first").click(function() { $boy.addClass('slowWalk').transition({ 'left': $("#content").width() + 'px', }, 10000); }); // 暂停 $("button:last").click(function() { var left = $boy.css('left'); // 强制做了一个改变目标left的处理 // 动画是要运行10秒,所以此时动画还是没有结束的 $boy.css('left',left); // 增加暂停的样式 // ? }); </script> </body> </html>
/*背景图*/ .a_background { width: 100%; height: 100%; position: absolute; } .a_background_top{ width: 100%; height: 71.6%; background-image: url("http://img1.sycdn.imooc.com//55addf6900019d8f14410645.png"); background-size: 100% 100%; } .a_background_middle{ width: 100%; height: 13.1%; background-image: url("http://img1.sycdn.imooc.com//55addf800001ff2e14410118.png"); background-size: 100% 100%; } .a_background_botton{ width: 100%; height: 15.3%; background-image: url("http://img1.sycdn.imooc.com//55addfcb000189b314410138.png"); background-size: 100% 100%; }
* { padding: 0; margin: 0; } ul, li { list-style-type: none; } /*主体部分*/ #content { width: 100%; height: 100%; /* top: 20%; */ overflow: hidden; position: absolute; } .content-wrap { position: relative; } .content-wrap > li { /* background: #CAE1FF; color: red; */ float: left; overflow: hidden; position: relative; } /* li:nth-child(2) { background: #9BCD9B; } li:nth-child(3) { background: yellow; } a { position: absolute; top: 50%; left: 40%; } */ .charector { left: 0%; top: 55%; position: absolute; /* width: 100%; height: 100%; */ width: 151px; height: 291px; background: url(http://img.mukewang.com/55ade248000198ae10550582.png) -0px -291px no-repeat; } .slowWalk { -webkit-animation-name: person-slow; -webkit-animation-duration: 950ms; -webkit-animation-iteration-count: infinite; -webkit-animation-timing-function: steps(1, start); -moz-animation-name: person-slow; -moz-animation-duration: 950ms; -moz-animation-iteration-count: infinite; -moz-animation-timing-function: steps(1, start) } /*普通慢走*/ @-webkit-keyframes person-slow { 0% { background-position: -0px -291px; } 25% { background-position: -602px -0px; } 50% { background-position: -302px -291px; } 75% { background-position: -151px -291px; } 100% { background-position: -0px -291px; } } @-moz-keyframes person-slow { 0% { background-position: -0px -291px; } 25% { background-position: -602px -0px; } 50% { background-position: -302px -291px; } 75% { background-position: -151px -291px; } 100% { background-position: -0px -291px; } }
///////// //页面滑动 // ///////// /** * [Swipe description] * @param {[type]} container [页面容器节点] * @param {[type]} options [参数] */ function Swipe(container) { // 获取第一个子节点 var element = container.find(":first"); var swipe = {}; // li页面数量 var slides = element.find("li"); //获取容器尺寸 var width = container.width(); var height = container.height(); //设置li页面总宽度 element.css({ width: (slides.length * width) + 'px', height: height + 'px' }); // 设置每一个页面li的宽度 $.each(slides, function(index) { var slide = slides.eq(index); // 获取到每一个li元素 slide.css({ width: width + 'px', height: height + 'px' }); }); // 监控完成与移动 swipe.scrollTo = function(x, speed) { // 执行动画移动 element.css({ 'transition-timing-function' : 'linear', 'transition-duration' : speed + 'ms', 'transform' : 'translate3d(-' + x + 'px,0px,0px)' }); return this; }; return swipe; }