第三个场景增加pageC.js与pageC.css2个文件,场景内的元素,月亮、星星、圣诞树和云的实现之前都已经讲解过了,这里不再重复。具体看下关窗的实现的与canvas版的雪花效果
关窗的同时,背景会有一个渐隐渐现的替换效果,这里会采用2张图做交替动画,一张隐藏一张显示,因此同时会有3个动画在进行
参考pageC.js与pageC.css代码区域:
背景图切换
在静态HTML布局中,给2张不同的切换节设置opacity属性控显示与隐藏的状态,
<div class="window-scene-bg"></div> //1 <div class="window-close-bg"></div> //0
在脚本代码处,同时给2个背景节点增加opacity的控制,这样达到渐隐渐现的切换效果
this.$sceneBg.transition({ opacity: 0, }, 3000); this.$closeBg.transition({ opacity: 1 }, 5000);
关窗动画
通过脚本控制给对应的“门”增加关门的样式,通过animation动画,控制元素的scale、rotateY鱼与rotateZ值的变化。
element.addClass("close").one("animationend webkitAnimationEnd", function(event) { complete() })
给左右2个门增加"close"样式控制,然后绑定一个one事件监听动画的处理完成,这样才能衔接到后续的动作了(这里需要注意animation是2组动画,需要判断下2组动画全部结束)
在pageC.js文件中的31行出填入任务代码
给左右2片窗户增加开窗效果,然后监听窗户的动作完毕后调用回调函数
提示:
this.$leftWin ,this.$rightWin 是左右2个窗户元素
class=close是关窗的样式,调用了css3的动画
css3的动画事件名是:animationend
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>圣诞主题</title> <link rel='stylesheet' href='common.css' /> <link rel="stylesheet" type="text/css" href="pageC.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> <script src="pageC.js"></script> <script src="christmas.js"></script> </head> <body> <section class="container"> <!-- 第一幅画面 --> <section class="page-a bg-adaptive"> </section> <!-- 第二幅画面 --> <section class="page-b bg-adaptive"> </section> <!-- 第三幅画面 --> <section class="page-c bg-adaptive"> <!-- 窗户关闭 --> <div class="window wood"> <div class="window-content" data-attr="red"> <div class="window-scene-bg"></div> <div class="window-close-bg"></div> <div class="window-left hover"></div> <div class="window-right hover"></div> </div> </div> </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 $pageC = $(".page-c"); //构建第三个场景页面对象 new pageC($pageC); }; $(function() { $("button").on("click",function(){ Christmas(); }) })
/** * 第二副场景页面 * */ function pageC() { this.$window = $(".page-c .window"); this.$leftWin = this.$window.find(".window-left"); this.$rightWin = this.$window.find(".window-right"); this.$sceneBg = this.$window.find(".window-scene-bg"); this.$closeBg = this.$window.find(".window-close-bg"); // 背景切换 this.$sceneBg.transition({ opacity: 0, }, 3000); this.$closeBg.css("transform", "translateZ(0)") this.$closeBg.transition({ opacity: 1 }, 5000); //关门动作 this.closeWindow(); } /** * 关闭窗 * @return {[type]} [description] */ pageC.prototype.closeWindow = function() { //? }
*{ margin: 0; padding: 0; } .container { width: 100%; height: 100%; position: relative; overflow: hidden; } .bg-adaptive { background-size: 100% 100%; }
.page-c { width : 100%; height : 100%; background-image: url("http://img1.sycdn.imooc.com//565d0b280001788014410901.png"); position: absolute; z-index: 30; } /** * 窗户 */ .window { width: 2.6rem; height: 1.5rem; position: absolute; left: 9.7rem; top: 6.2rem; cursor: pointer; -webkit-perspective: 500px; -moz-perspective: 500px; } .window-content { -webkit-transform-style: preserve-3d; -moz-transform-style: preserve-3d; width: 91%; margin: 0 auto; height: 100%; overflow: hidden; } /** * 窗户背景 */ .window-bg { width: 86%; height: 92%; position: absolute; left: 50%; margin-left: -43%; bottom: 0; background: url(http://img.mukewang.com/565d07770001790814410901.png); background-size: 100% 100%; z-index: -1; } /** * 窗户底边 * @type {[type]} */ .window:before { content: ""; background: url(http://img.mukewang.com/565d07e40001088402410017.png); width: 100%; height: 0.17rem; display: block; position: absolute; bottom: 0.05rem; background-size: 100% 100%; z-index: 100; } /** * 底边阴影 * @type {[type]} */ .window:after { content: ""; background: url(http://img.mukewang.com/565d080400018d2702270009.png); width: 100%; height: 0.09rem; display: block; position: absolute; bottom: 0; background-size: 100% 100%; z-index: 100; } .wood { display: block; overflow: hidden; } /** * 左侧门 */ .window-left { float: left; background: url(http://img.mukewang.com/565d081d0001cfd901140134.png); -webkit-border-top-left-radius: 0.1rem; -moz-border-top-left-radius: 0.1rem; } /** * 右侧门 */ .window-right { float: right; background: url(http://img.mukewang.com/565d084c0001431b01140134.png); -webkit-border-top-right-radius: 0.1rem; -moz-border-top-left-radius: 0.1rem; } .window-left, .window-right { width: 50%; height: 1.3rem; z-index: 110; box-shadow: 0 0 0.15rem #FCF0BC; background-size: 100% 100%; } .window-left.hover { -webkit-transform: scale(0.95) rotateY(60deg) rotateZ(2deg); -moz-transform: scale(0.95) rotateY(60deg) rotateZ(2deg); margin-top: 0.1rem; margin-left: -0.25rem; } .window-right.hover { -webkit-transform: scale(0.95) rotateY(-60deg) rotateZ(-2deg); -moz-transform: scale(0.95) rotateY(-60deg) rotateZ(-2deg); margin-top: 0.1rem; margin-right: -0.25rem; } /** * 窗户 */ .page-c .window { left: 8rem; } .window-left.close, .window-right.close { -webkit-animation: closeWindow 2s forwards; -moz-animation: closeWindow 2s forwards; } @-webkit-keyframes closeWindow { 100% { -webkit-transform: scale(1) rotateY(0deg) rotateZ(0deg); margin-right: 0; margin-left: 0; } } @-moz-keyframes closeWindow { 100% { -moz-transform: scale(1) rotateY(0deg) rotateZ(0deg); margin-right: 0; margin-left: 0; } } /** * 场景背景 * @type {[type]} */ .window-scene-bg { background: url(http://img.mukewang.com/565d0b4c0001816b02270135.png); background-size: 100% 100%; width: 2.26rem; height: 1.2rem; position: absolute; left: 50%; bottom: .1rem; z-index: -1; margin-left: -1.13rem; -webkit-transform: translateZ(-50px); -moz-transform: translateZ(-50px); } /** * 关门背景 */ .window-close-bg { background: url(http://img.mukewang.com/565d0b3d00016a4600810081.png); background-size: 100% 100%; width: 0.8rem; height: 0.8rem; position: absolute; left: 50%; bottom: .1rem; margin-left: -0.4rem; -webkit-transform: translateZ(-50px); -moz-transform: translateZ(-50px); opacity: 0; z-index: 500; }