在第一副页面中,雪橇驮着小男孩从远方渐渐的飞到小屋窗前,雪橇运动变化就是关键帧的精灵动画与transition的结合的效果
参考右边代码:christmas.js中调用new pageA场景类。pageA.js中的场景类pageA,定义了next、run与stopWalk方法,都是用来控制雪橇动画的
雪橇的逻辑:
值得注意的是:在代码的编写上,我合并了next方法的处理,增加了Deferred异步的方法,让异步的代码可以有同步执行的逻辑,这样代码结构看起来会更清晰
在next方法中封装了Deferred是使用,所以可以连续调用
next().then().then().then().......
具体可以参考3-6异步编程那一节,通过jQuery的Deferred对象改造异步流程
在pageA.js中64行处填入任务代码
参考next方法,填充雪橇运行的最后一段距离
在7000毫秒的时间内,继续飞行到坐标top = 7.8rem,right=1.2rem的位置
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>圣诞主题</title> <link rel='stylesheet' href='common.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> <link rel="stylesheet" type="text/css" href="pageA.css"> <script src="pageA.js"></script> <script src="christmas.js"></script> </head> <body> <section class="container"> <!-- 第一幅画面 --> <section class="page-a bg-adaptive"> <!-- 男孩 --> <div class="chs-boy chs-boy-deer"></div> </section> <!-- 第二幅画面 --> <section class="page-b bg-adaptive"> </section> <!-- 第三幅画面 --> <section class="page-c bg-adaptive"> </section> </section> <button>点击运行雪橇动作</button> <script type="text/javascript"> //rem设置 (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', recalc = function() { var clientWidth = docEl.clientWidth; if (!clientWidth) return; docEl.style.fontSize = 20 * (clientWidth / 320) + 'px'; //宽与高度 document.body.style.height = clientWidth * (900 / 1440) + "px" }; win.addEventListener(resizeEvt, recalc, false); doc.addEventListener('DOMContentLoaded', recalc, false); })(document, window); </script> </body> </html>
/** * 慕课网特制 * 圣诞主题效果 * @type {Object} */ /** * 中间调用 */ var Christmas = function() { //页面容器元素 var $pageA = $(".page-a"); //构建第一个场景页面对象 new pageA($pageA); }; $(function() { $("button").click(function(){ //圣诞主题效果,开始 Christmas() }) })
/** * 第一副场景页面 * @param {[type]} argument [description] * @return {[type]} [description] */ function pageA (element) { //根元素 this.$root = element; //小男孩 this.$boy = element.find(".chs-boy"); //运行动画 this.run(); } /** * 运行下一个动画 * @return {Function} [description] */ pageA.prototype.next = function(options) { var dfd = $.Deferred(); this.$boy.transition(options.style, options.time, "linear",function() { dfd.resolve() }); return dfd; } /** * 停止走路 * @return {[type]} [description] */ pageA.prototype.stopWalk = function(){ this.$boy.removeClass("chs-boy-deer") } /** * 路径 * @return {[type]} [description] */ pageA.prototype.run = function(callback){ var that = this; var next = function() { return this.next.apply(this, arguments) }.bind(this) next({ "time": 10000, "style": { "top": "4rem", "right": "16rem", "scale": "1.2" } }) .then(function() { return next({ "time":500, "style": { "rotateY" : "-180", "scale": "1.5" } }) }) //? .then(function(){ that.stopWalk(); }) }
*{ margin: 0; padding: 0; } .container { width: 100%; height: 100%; position: relative; overflow: hidden; } .bg-adaptive { background-size: 100% 100%; }
/** * 背景布置 */ .container .page-a { width : 100%; height : 100%; background-image: url("http://img1.sycdn.imooc.com//565d07770001790814410901.png"); position: absolute; z-index: 5; } /** * 圣诞男孩 */ .chs-boy { width : 5rem; height : 1.5rem; position : absolute; z-index : 3; top : .1rem; right : -3rem; transform : scale(0.1); background : url(http://img.mukewang.com/565d07490001365329660269.png) -300% -100%; background-size : 400% 100%; } /** * 男孩走路动作 */ .chs-boy-deer { -webkit-animation:chsBoyDeer 0.75s steps(3,end) infinite; -moz-animation:chsBoyDeer 0.75s steps(3,end) infinite; } @-webkit-keyframes chsBoyDeer { 0% { background-position: -0% 100%; } 100% { background-position: -300% 100%; } } @-moz-keyframes chsBoyDeer { 0% { background-position: -0% 100%; } 100% { background-position: -300% 100%; } } /** * 人物停止 */ .boy-stop-animate { -webkit-animation-play-state: paused; }