6-3 灯光效果
本节编程练习不计算学习进度,请电脑登录imooc.com操作

灯光效果

在开门的同时会有一个灯光的变化,开门灯亮,关门灯灭,这个动作本身不难,主要是配合开关门之后的一个效果,需要依赖开关门的事件回调

由于开关门的代码封装了Deferred,我们很容易添加这个效果的逻辑

灯的初始效果是暗的状态,所以需要在PageB.css部分增加一个lamp-bright的样式(一张点亮的背景图),通过增加删除这个样式达到变化的效果

这里唯一要注意的问题就是大图在第一加载时需要时间的,在某些环境下会出现第一次加载闪动的情况,这里增加一个不相关的节点b_background_preload做预加载

代码实现部分:

//开门
openDoor().then(function() {
    //开灯
    lamp.bright();
})
//关门
shutDoor().then(function() {
    //灯灭
    lamp.dark();
});

通过融入Deferred对象,我们可以很好的控制这个逻辑了,在then中直接书写回调后的开灯效果了。

通过点击开门、关门的按钮,可以观察到效果了

任务

打开index.html文件,在代码的138行填入相应代码,可以观察到关灯的效果

lamp.dark();
  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. <link rel='stylesheet' href='pageB.css' />
  10. <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
  11. <script type="text/javascript" src="http://img1.sycdn.imooc.com//down/55ac9ea30001ace700000000.js"></script>
  12. <script type="text/javascript" src="Swipe.js"></script>
  13. <script type="text/javascript" src="BoyWalk.js"></script>
  14. </head>
  15.  
  16. <body>
  17. <div id='content'>
  18. <ul class='content-wrap'>
  19. <!-- 第一副画面 -->
  20. <li>
  21. <!-- 背景 -->
  22. <div class="a_background">
  23. <div class="a_background_top"></div>
  24. <div class="a_background_middle"></div>
  25. <div class="a_background_botton"></div>
  26. </div>
  27. <!-- 云 -->
  28. <div class="cloudArea">
  29. <div class="cloud"></div>
  30. <div class="cloud"></div>
  31. </div>
  32. <!-- 太阳 -->
  33. <div id="sun"></div>
  34. </li>
  35. <!-- 第二副画面 -->
  36. <li>
  37. <!-- 背景图 -->
  38. <div class="b_background"></div>
  39. <div class="b_background_preload"></div>
  40. <!-- 商店 -->
  41. <div class="shop">
  42. <div class="door">
  43. <div class="door-left"></div>
  44. <div class="door-right"></div>
  45. </div>
  46. <!-- 灯 -->
  47. <div class="lamp"></div>
  48. </div>
  49. </li>
  50. <li> 页面3 </li>
  51. </ul>
  52. <div class="button">
  53. <button>开门亮灯</button>
  54. <button>关门灯灭</button>
  55. </div>
  56. </div>
  57. </body>
  58. <script type="text/javascript">
  59. $(function() {
  60.  
  61. var container = $("#content");
  62. var swipe = Swipe(container);
  63. // 页面滚动到指定的位置
  64. function scrollTo(time, proportionX) {
  65. var distX = container.width() * proportionX;
  66. swipe.scrollTo(distX, time);
  67. }
  68.  
  69. ////////////////////////////////////////////////////////
  70. // ================= 动画处理 ======================= //
  71. ////////////////////////////////////////////////////////
  72.  
  73. // 用来临时调整页面
  74. swipe.scrollTo(container.width(), 0);
  75.  
  76.  
  77. function doorAction(left, right, time) {
  78. var $door = $('.door');
  79. var doorLeft = $('.door-left');
  80. var doorRight = $('.door-right');
  81. var defer = $.Deferred();
  82. var count = 2;
  83. // 等待开门完成
  84. var complete = function() {
  85. if (count == 1) {
  86. defer.resolve();
  87. return;
  88. }
  89. count--;
  90. }
  91. doorLeft.transition({
  92. 'left': left
  93. }, time, complete);
  94. doorRight.transition({
  95. 'left': right
  96. }, time, complete);
  97. return defer;
  98. }
  99.  
  100. // 开门
  101. function openDoor() {
  102. return doorAction('-50%', '100%', 2000);
  103. }
  104.  
  105. // 关门
  106. function shutDoor() {
  107. return doorAction('0%', '50%', 2000);
  108. }
  109.  
  110.  
  111. ///////////
  112. // 灯动画 //
  113. ///////////
  114. var lamp = {
  115. elem: $('.b_background'),
  116. bright: function() {
  117. this.elem.addClass('lamp-bright')
  118. },
  119. dark: function() {
  120. this.elem.removeClass('lamp-bright')
  121. }
  122. };
  123.  
  124. // 开门
  125. $("button:first").click(function() {
  126. // 开门
  127. openDoor().then(function() {
  128. // 开灯
  129. lamp.bright();
  130. });
  131. });
  132.  
  133. // 关门
  134. $("button:last").click(function() {
  135. // 关门
  136. shutDoor().then(function() {
  137. // 灯灭
  138. // ?
  139. });
  140.  
  141. });
  142.  
  143.  
  144. })
  145. </script>
  146.  
  147. </html>
  1. /////////
  2. //页面滑动 //
  3. /////////
  4.  
  5.  
  6. /**
  7.  * [Swipe description]
  8.  * @param {[type]} container [页面容器节点]
  9.  * @param {[type]} options [参数]
  10.  */
  11. function Swipe(container) {
  12. // 获取第一个子节点
  13. var element = container.find(":first");
  14. var swipe = {};
  15.  
  16. // li页面数量
  17. var slides = element.find("li");
  18.  
  19. // 获取容器尺寸
  20. var width = container.width();
  21. var height = container.height();
  22.  
  23. // 设置li页面总宽度
  24. element.css({
  25. width: (slides.length * width) + 'px',
  26. height: height + 'px'
  27. })
  28.  
  29. // 设置每一个页面li的宽度
  30. $.each(slides, function(index) {
  31. var slide = slides.eq(index); // 获取到每一个li元素
  32. slide.css({
  33. width: width + 'px',
  34. height: height + 'px'
  35. });
  36. });
  37.  
  38. // 监控完成与移动
  39. swipe.scrollTo = function(x, speed) {
  40. // 执行动画移动
  41. element.css({
  42. 'transition-timing-function' : 'linear',
  43. 'transition-duration' : speed + 'ms',
  44. 'transform' : 'translate3d(-' + x + 'px,0px,0px)'
  45. });
  46. return this;
  47. }
  48.  
  49. return swipe;
  50. }
  1. * {
  2. padding: 0;
  3. margin: 0;
  4. }
  5.  
  6. ol,
  7. ul,
  8. li {
  9. list-style-type: none;
  10. }
  11. /*主体部分*/
  12.  
  13. #content {
  14. width: 100%;
  15. height: 100%;
  16. /* top: 20%; */
  17. overflow: hidden;
  18. position: absolute;
  19. }
  20.  
  21. .content-wrap {
  22. position: relative;
  23. }
  24.  
  25. .content-wrap > li {
  26. background: #CAE1FF;
  27. color: red;
  28. float: left;
  29. overflow: hidden;
  30. position: relative;
  31. }
  32.  
  33. li:nth-child(2) {
  34. background: #9BCD9B;
  35. }
  36.  
  37. li:nth-child(3) {
  38. background: yellow;
  39. }
  40.  
  41. a {
  42. position: absolute;
  43. top: 50%;
  44. left: 40%;
  45. }
  46.  
  47. .charector {
  48. position: absolute;
  49. left: 0%;
  50. top: 55%;
  51. position: absolute;
  52. width: 100%;
  53. height: 100%;
  54. width: 151px;
  55. height: 291px;
  56. background: url(http://img.mukewang.com/55ade248000198ae10550582.png) -0px -291px no-repeat;
  57. }
  58.  
  59. .slowWalk {
  60. -webkit-animation-name: person-slow;
  61. -webkit-animation-duration: 950ms;
  62. -webkit-animation-iteration-count: infinite;
  63. -webkit-animation-timing-function: steps(1, start);
  64. -moz-animation-name: person-slow;
  65. -moz-animation-duration: 950ms;
  66. -moz-animation-iteration-count: infinite;
  67. -moz-animation-timing-function: steps(1, start)
  68. }
  69. /*普通慢走*/
  70.  
  71. @-webkit-keyframes person-slow {
  72. 0% {
  73. background-position: -0px -291px;
  74. }
  75. 25% {
  76. background-position: -602px -0px;
  77. }
  78. 50% {
  79. background-position: -302px -291px;
  80. }
  81. 75% {
  82. background-position: -151px -291px;
  83. }
  84. 100% {
  85. background-position: -0px -291px;
  86. }
  87. }
  88.  
  89. @-moz-keyframes person-slow {
  90. 0% {
  91. background-position: -0px -291px;
  92. }
  93. 25% {
  94. background-position: -602px -0px;
  95. }
  96. 50% {
  97. background-position: -302px -291px;
  98. }
  99. 75% {
  100. background-position: -151px -291px;
  101. }
  102. 100% {
  103. background-position: -0px -291px;
  104. }
  105. }
  1. /*背景图*/
  2.  
  3. .a_background {
  4. width: 100%;
  5. height: 100%;
  6. /* background-image: url("../images/QixiA.png");
  7.   background-size: 100% 100%;*/
  8.  
  9. position: absolute;
  10. }
  11.  
  12. .a_background_top {
  13. width: 100%;
  14. height: 71.6%;
  15. background-image: url("http://img1.sycdn.imooc.com//55addf6900019d8f14410645.png");
  16. background-size: 100% 100%;
  17. }
  18.  
  19. .a_background_middle {
  20. width: 100%;
  21. height: 13.1%;
  22. background-image: url("http://img1.sycdn.imooc.com//55addf800001ff2e14410118.png");
  23. background-size: 100% 100%;
  24. }
  25.  
  26. .a_background_botton {
  27. width: 100%;
  28. height: 15.3%;
  29. background-image: url("http://img1.sycdn.imooc.com//55addfcb000189b314410138.png");
  30. background-size: 100% 100%;
  31. }
  32.  
  33. button {
  34. width: 80px;
  35. height: 50px;
  36. }
  37.  
  38. .button {
  39. position: absolute;
  40. bottom: 0;
  41. }
  42. /*人物暂停*/
  43.  
  44. .pauseWalk {
  45. animation-play-state: paused;
  46. }
  47. /*-------- 太阳自转以及动画 --------*/
  48.  
  49. #sun {
  50. background: url("http://img1.sycdn.imooc.com//55ade004000106c202010201.png") no-repeat;
  51. position: absolute;
  52. z-index: 1;
  53. top: -30px;
  54. height: 201px;
  55. width: 201px;
  56. right: 40%;
  57. }
  58.  
  59. .rotation {
  60. -webkit-animation-name: rotation;
  61. -webkit-animation-duration: 30s;
  62. -webkit-animation-iteration: 1;
  63. -moz-animation-name: rotation;
  64. -moz-animation-duration: 30s;
  65. -moz-animation-iteration: 1;
  66. }
  67.  
  68. @-webkit-keyframes rotation {
  69. 0% {
  70. transform: translateX(0) translateY(0);
  71. }
  72. 100% {
  73. transform: translateX(-2000px) translateY(400px);
  74. }
  75. }
  76.  
  77. @-moz-keyframes rotation {
  78. 0% {
  79. transform: translateX(0) translateY(0);
  80. }
  81. 100% {
  82. transform: translateX(-2000px) translateY(400px);
  83. }
  84. }
  85. /*天空云*/
  86.  
  87. .cloud {
  88. z-index: 2;
  89. position: absolute;
  90. }
  91.  
  92. .cloud1 {
  93. width: 20%;
  94. height: 30%;
  95. left: -5%;
  96. top: 15%;
  97. background: url("http://img1.sycdn.imooc.com//55addfde0001aec501810101.png") no-repeat;
  98. -webkit-animation-name: smallCloud;
  99. -webkit-animation-duration: 30s;
  100. -webkit-animation-iteration: infinite;
  101. -moz-animation-name: smallCloud;
  102. -moz-animation-duration: 30s;
  103. -moz-animation-iteration: infinite
  104. }
  105.  
  106. .cloud2 {
  107. width: 20%;
  108. height: 30%;
  109. right: -5%;
  110. background: url("http://img1.sycdn.imooc.com//55addff500016df503010140.png") no-repeat;
  111. -webkit-animation-name: largeCloud;
  112. -webkit-animation-duration: 60s;
  113. -webkit-animation-iteration: infinite;
  114. -moz-animation-name: largeCloud;
  115. -moz-animation-duration: 60s;
  116. -moz-animation-iteration: infinite;
  117. }
  118.  
  119. @-webkit-keyframes smallCloud {
  120. 0% {
  121. left: -5%;
  122. }
  123. 100% {
  124. left: 100%;
  125. }
  126. }
  127.  
  128. @-moz-keyframes smallCloud {
  129. 0% {
  130. left: -5%;
  131. }
  132. 100% {
  133. left: 100%;
  134. }
  135. }
  136.  
  137. @-webkit-keyframes largeCloud {
  138. 0% {
  139. right: -5%;
  140. }
  141. 100% {
  142. right: 100%;
  143. }
  144. }
  145.  
  146. @-moz-keyframes largeCloud {
  147. 0% {
  148. right: -5%;
  149. }
  150. 100% {
  151. right: 100%;
  152. }
  153. }
  1. /*背景图*/
  2.  
  3. .b_background {
  4. width: 100%;
  5. height: 100%;
  6. background-image: url("http://img1.sycdn.imooc.com//55ade06f00015a0d14410901.png");
  7. background-size: 100% 100%;
  8. position: absolute;
  9. }
  10.  
  11. .b_background_preload {
  12. background: url("http://img1.sycdn.imooc.com//55ade0be0001a37914410901.png") no-repeat -9999px -9999px;
  13. }
  14.  
  15. .lamp-bright {
  16. background-image: url("http://img1.sycdn.imooc.com//55ade0be0001a37914410901.png");
  17. }
  18.  
  19.  
  20. /*商店*/
  21.  
  22. .shop {
  23. width: 39.5%;
  24. height: 35.5%;
  25. position: absolute;
  26. left: 29%;
  27. top: 36.5%;
  28. }
  29.  
  30. .door {
  31. position: absolute;
  32. width: 32%;
  33. height: 100%;
  34. top: 32%;
  35. height: 68%;
  36. overflow: hidden;
  37. left: 58.5%;
  38. }
  39.  
  40. .door-left,
  41. .door-right {
  42. width: 50%;
  43. height: 100%;
  44. position: absolute;
  45. }
  46.  
  47. .door-left {
  48. left: 0%;
  49. background: url(http://img.mukewang.com/55ade1140001050d00910231.png);
  50. background-size: 100% 100%;
  51. }
  52.  
  53. .door-right {
  54. left: 50%;
  55. background: url(http://img.mukewang.com/55ade12100019f5b00910231.png);
  56. background-size: 100% 100%;
  57. }
下一节