飞鸟动画也跟小男孩动画一样,通过精灵与transition的组合实现
在页面增加一个鸟的HTML结构
<div class="bird"></div>
CSS布局比较简单,通过background-position加载精灵图,做动画的元素都是需要设置position:absolute这样才能独立漂浮文档流,让页面的重绘更少
图片变化部分采用的是CSS3的animation,通过设置animation-timing-function: step-start;马上跳到动画每一帧结束的状态,这样就让动画执行一帧一帧的切换效果
@-webkit-keyframes bird-slow { 0% { background-position: -182px 0px; } 50% {background-position: 0px 0px;} 75% {background-position: -91px 0px;} 100% {background-position: -182px 0px;} }
以上是4个变化点,但是实际上我们只有3张图,0% 100%是最后一帧,这是因为设置step-start了的缘故,注意下这个写法就可以了
移动部分就很简单,我们移动left或者right的值,距离就是一个页面单位,注意下正负取值
具体的实现,可以参考下源码部分
在pageB.css文件中,代码第87和92行分别填写代码,实现鸟飞的动作
0% { background-position: -182px 0px; } 50% { background-position: 0px 0px; } 75% { background-position: -91px 0px; } 100% { background-position: -182px 0px; }
<!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' /> <link rel='stylesheet' href='pageB.css' /> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script> <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.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> <!-- 云 --> <div class="cloudArea"> <div class="cloud"></div> <div class="cloud"></div> </div> <!-- 太阳 --> <div id="sun"></div> </li> <!-- 第二副画面 --> <li> <!-- 背景图 --> <div class="b_background"></div> <div class="b_background_preload"></div> <!-- 商店 --> <div class="shop"> <div class="door"> <div class="door-left"></div> <div class="door-right"></div> </div> <!-- 灯 --> <div class="lamp"></div> </div> <!-- 鸟 --> <div class="bird"></div> </li> <li> 页面3 </li> </ul> <div id="boy" class="charector"></div> </div> <div class="button"> <button>开始</button> </div> </body> <script type="text/javascript"> $(function() { var container = $("#content"); var swipe = Swipe(container); // 页面滚动到指定的位置 function scrollTo(time, proportionX) { var distX = container.width() * proportionX; swipe.scrollTo(distX, time); } //////////////////////////////////////////////////////// // ================= 动画处理 ======================= // //////////////////////////////////////////////////////// // 用来临时调整页面 swipe.scrollTo(container.width(), 0); var boy = BoyWalk() ///////// //右边飞鸟 // ///////// var bird = { elem: $(".bird"), fly: function() { this.elem.addClass('birdFly') this.elem.transition({ right: container.width() }, 15000, 'linear'); } }; function startRun() { boy.walkTo(2000, 0.5) .then(function() { //暂停走路 boy.stopWalk() }) .then(function() { // 开门 return openDoor(); }) .then(function() { // 开灯 lamp.bright(); }) .then(function() { // 进商店 return boy.toShop(2000); }).then(function(){ // 取花 return boy.talkFlower(); }).then(function() { // 飞鸟 bird.fly(); }).then(function() { // 出商店 return boy.outShop(2000); }).then(function(){ // 关门 return shutDoor(); }).then(function() { // 灯暗 lamp.dark(); }); } // 开始 $("button:first").click(startRun); }) </script> <script type="text/javascript" src="Swipe.js"></script> <script type="text/javascript" src="Qixi.js"></script> </html>
/////////// //灯动画 // /////////// var lamp = { elem: $('.b_background'), bright: function() { this.elem.addClass('lamp-bright'); }, dark: function() { this.elem.removeClass('lamp-bright'); } }; function doorAction(left, right, time) { var $door = $('.door'); var doorLeft = $('.door-left'); var doorRight = $('.door-right'); var defer = $.Deferred(); var count = 2; // 等待开门完成 var complete = function() { if (count == 1) { defer.resolve(); return; } count--; }; doorLeft.transition({ 'left': left }, time, complete); doorRight.transition({ 'left': right }, time, complete); return defer; } // 开门 function openDoor() { return doorAction('-50%', '100%', 2000); } // 关门 function shutDoor() { return doorAction('0%', '50%', 2000); } var instanceX; /** * 小孩走路 * @param {[type]} container [description] */ function BoyWalk() { var container = $("#content"); // 页面可视区域 var visualWidth = container.width(); var visualHeight = container.height(); // 获取数据 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 boyWidth = $boy.width(); var boyHeight = $boy.height(); //设置下高度 $boy.css({ top: pathY - boyHeight + 25 }); // 暂停走路 function pauseWalk() { $boy.addClass('pauseWalk'); } // 恢复走路 function restoreWalk() { $boy.removeClass('pauseWalk'); } // css3的动作变化 function slowWalk() { $boy.addClass('slowWalk'); } // 用transition做运动 function stratRun(options, runTime) { var dfdPlay = $.Deferred(); // 恢复走路 restoreWalk(); // 运动的属性 $boy.transition( options, runTime, 'linear', function() { dfdPlay.resolve(); // 动画完成 }); return dfdPlay; } // 开始走路 function walkRun(time, dist, disY) { time = time || 3000; // 脚动作 slowWalk(); // 开始走路 var d1 = stratRun({ 'left': dist + 'px', 'top': disY ? disY : undefined }, time); return d1; } // 走进商店 function walkToShop(runTime) { var defer = $.Deferred(); var doorObj = $('.door') // 门的坐标 var offsetDoor = doorObj.offset(); var doorOffsetLeft = offsetDoor.left; // 小孩当前的坐标 var offsetBoy = $boy.offset(); var boyOffetLeft = offsetBoy.left; // 当前需要移动的坐标 instanceX = (doorOffsetLeft + doorObj.width() / 2) - (boyOffetLeft + $boy.width() / 2); // 开始走路 var walkPlay = stratRun({ transform: 'translateX(' + instanceX + 'px),scale(0.3,0.3)', opacity: 0.1 }, 2000); // 走路完毕 walkPlay.done(function() { $boy.css({ opacity: 0 }) defer.resolve(); }) return defer; } // 走出店 function walkOutShop(runTime) { var defer = $.Deferred(); restoreWalk(); // 开始走路 var walkPlay = stratRun({ transform: 'translateX(' + instanceX + 'px),translateY(0),,scale(1,1)', opacity: 1 }, runTime); // 走路完毕 walkPlay.done(function() { defer.resolve(); }) return defer; } // 取花 function talkFlower() { // 增加延时等待效果 var defer = $.Deferred(); setTimeout(function() { // 取花 $boy.addClass('slowFlolerWalk'); defer.resolve(); }, 1000); return defer; } // 计算移动距离 function calculateDist(direction, proportion) { return (direction == "x" ? visualWidth : visualHeight) * proportion; } return { // 开始走路 walkTo: function(time, proportionX, proportionY) { var distX = calculateDist('x', proportionX) var distY = calculateDist('y', proportionY) return walkRun(time, distX, distY); }, // 走进商店 toShop: function() { return walkToShop.apply(null, arguments); }, // 走出商店 outShop: function() { return walkOutShop.apply(null, arguments); }, // 停止走路 stopWalk: function() { pauseWalk(); }, setColoer: function(value) { $boy.css('background-color', value); }, // 取花 talkFlower: function() { return talkFlower(); } } }
///////// //页面滑动 // ///////// /** * [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; }
* { padding: 0; margin: 0; } ol, 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; }a li:nth-child(3) { background: yellow; } a { position: absolute; top: 50%; left: 40%; } .charector { position: absolute; 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; } /*人物暂停*/ .pauseWalk { -webkit-animation-play-state: paused; } .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) } .slowFlolerWalk { -webkit-animation-name: person-floler-slow; -webkit-animation-duration: 950ms; -webkit-animation-iteration-count: infinite; -webkit-animation-timing-function: step-start; -moz-animation-name: person-floler-slow; -moz-animation-duration: 950ms; -moz-animation-iteration-count: infinite; -moz-animation-timing-function: step-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; } } /*带花*/ @-webkit-keyframes person-floler-slow { 0% { background-position: -453px -0px; } 25% { background-position: -904px -0px; } 50% { background-position: -451px -0px; } 75% { background-position: -753px -0px; } 100% { background-position: -300px -0px; } } @-moz-keyframes person-floler-slow { 0% { background-position: -453px -0px; } 25% { background-position: -904px -0px; } 50% { background-position: -451px -0px; } 75% { background-position: -753px -0px; } 100% { background-position: -300px -0px; } }
/*背景图*/ .a_background { width: 100%; height: 100%; /* background-image: url("../images/QixiA.png"); background-size: 100% 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%; } button { width: 80px; height: 50px; } .button { position: absolute; bottom: 0; } /*人物暂停*/ .pauseWalk { animation-play-state: paused; } /*-------- 太阳自转以及动画 --------*/ #sun { background: url("http://img1.sycdn.imooc.com//55ade004000106c202010201.png") no-repeat; position: absolute; z-index: 1; top: -30px; height: 201px; width: 201px; right: 40%; } .rotation { -webkit-animation-name: rotation; -webkit-animation-duration: 30s; -webkit-animation-iteration: 1; -moz-animation-name: rotation; -moz-animation-duration: 30s; -moz-animation-iteration: 1; } @-webkit-keyframes rotation { 0% { transform: translateX(0) translateY(0); } 100% { transform: translateX(-2000px) translateY(400px); } } @-moz-keyframes rotation { 0% { transform: translateX(0) translateY(0); } 100% { transform: translateX(-2000px) translateY(400px); } } /*天空云*/ .cloud { z-index: 2; position: absolute; } .cloud1 { width: 20%; height: 30%; left: -5%; top: 15%; background: url("http://img1.sycdn.imooc.com//55addfde0001aec501810101.png") no-repeat; -webkit-animation-name: smallCloud; -webkit-animation-duration: 30s; -webkit-animation-iteration: infinite; -moz-animation-name: smallCloud; -moz-animation-duration: 30s; -moz-animation-iteration: infinite } .cloud2 { width: 20%; height: 30%; right: -5%; background: url("http://img1.sycdn.imooc.com//55addff500016df503010140.png") no-repeat; -webkit-animation-name: largeCloud; -webkit-animation-duration: 60s; -webkit-animation-iteration: infinite; -moz-animation-name: largeCloud; -moz-animation-duration: 60s; -moz-animation-iteration: infinite; } @-webkit-keyframes smallCloud { 0% { left: -5%; } 100% { left: 100%; } } @-moz-keyframes smallCloud { 0% { left: -5%; } 100% { left: 100%; } } @-webkit-keyframes largeCloud { 0% { right: -5%; } 100% { right: 100%; } } @-moz-keyframes largeCloud { 0% { right: -5%; } 100% { right: 100%; } }
/*背景图*/ .b_background { width: 100%; height: 100%; background-image: url("http://img1.sycdn.imooc.com//55ade06f00015a0d14410901.png"); background-size: 100% 100%; position: absolute; } .b_background_preload { background: url("http://img1.sycdn.imooc.com//55ade0be0001a37914410901.png") no-repeat -9999px -9999px; } .lamp-bright { background-image: url("http://img1.sycdn.imooc.com//55ade0be0001a37914410901.png"); } /*商店*/ .shop { width: 39.5%; height: 35.5%; position: absolute; left: 29%; top: 36.5%; } .door { position: absolute; width: 32%; height: 100%; top: 32%; height: 68%; overflow: hidden; left: 58.5%; } .door-left, .door-right { width: 50%; height: 100%; position: absolute; } .door-left { left: 0%; background: url(http://img.mukewang.com/55ade1140001050d00910231.png); background-size: 100% 100%; } .door-right { left: 50%; background: url(http://img.mukewang.com/55ade12100019f5b00910231.png); background-size: 100% 100%; } /*鸟*/ .bird { min-width: 91px; min-height: 71px; top: 10%; position: absolute; z-index: 10; right: -91px; background: url(http://img.mukewang.com/55ade1700001b38302730071.png) -182px 0px no-repeat; } .birdFly { -webkit-animation-name: bird-slow; -webkit-animation-duration: 400ms; -webkit-animation-timing-function: step-start; -webkit-animation-iteration-count: infinite; -moz-animation-name: bird-slow; -moz-animation-duration: 400ms; -moz-animation-timing-function: step-start; -moz-animation-iteration-count: infinite; } /*鸟慢飞*/ @-webkit-keyframes bird-slow { /* 鸟飞的动作 */ } @-moz-keyframes bird-slow { /* 鸟飞的动作 */ }