6-4 人物进出商店的坐标计算
本节编程练习不计算学习进度,请电脑登录imooc.com操作

人物进出商店的坐标计算

小男孩与商店在布局上是没有关系的,只是都是相对的靠近中间位置动作,那么随着分辨率的变化,二者之间的位置都有不同的变化,这里都是采用JS动态计算的。

创建一个Qixi.js,把BoyWalk.js等一些逻辑代码全都放到这个里面,这个就是主文件代码了,之后会把所有的代码都会合并方到这个文件里面

小男孩进出商店的走路还是属于小男孩自身的一个动作,所以代码最终要封装到BoyWalk中。在Qixi.js中找到BoyWalk代码部分,在进出商店部分增加了toShop与outShop两个接口,具体的实现部分没有采用变化left与top,而用了translateX,主要是学习为目的,采用互通是实现搭配

小男孩走进门的中间位置,具体的算法比较简单:

translateX = 门中间的left值 - 小男孩中间的left值
translateY = 人物底部的top值 - 门中间的top值

这里的取值采用jQuery的offset处理的,注意下position与offset的取值不同点,.offset()是相对于文档(document)的当前位置,.position()是相对于父级元素的位移,一个元素可以嵌套多个position所以这里要特别注意下。

在实际的的进门路线的处理中,代码是采用translateX + scale的组合,并没有采用translateY,原因就是scale是一个缩放效果,在实际上会有替代translateY这个Y轴变化的感觉,具体我们可以参考代码部分的处理

具体,我们参考代码部分walkToShop与walkOutShop两个实现方法

//开始走路
var walkPlay = stratRun({
    transform: 'translateX,scale',//代码略
    opacity: 0.1
}, 2000);
//走路完毕
walkPlay.done(function() {
    $boy.css({
        opacity: 0
    })
    defer.resolve();
})

在走路完毕后,监听了一个done的成功方法,用来设置人物的隐藏,这个是Deferred的一个接口,与then效果一致

备注:

特别注意下,很多时候大家都是通过left与top修改元素的页面坐标,那么left与top确实也是页面元素的一个准确的坐标值。这样的情况直到css3的transform的出现,因为会有一个translate同样能改变页面的坐标

所以如果运用了translate的元素,最终的坐标需要加上这个属性的值

元素的页面X坐标 = left + translateX
元素的页面Y坐标 = top + translateY

所以在这个案例上,小孩男运用了left与translateX,所以实现的X坐标值 = left + translateX 之和了。

任务

打开Qixi.js文件,在代码的162行填入相应代码,可以观察到人物走路商店的动作

var defer = $.Deferred();
restoreWalk();
//开始走路
var walkPlay = stratRun({
    transform: 'translateX(' + instanceX + 'px),scale(1,1)',
   opacity: 1
}, runTime);
//走路完毕
walkPlay.done(function() {
    defer.resolve();
});
return defer;          
  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. </head>
  13.  
  14. <body>
  15. <div id='content'>
  16. <ul class='content-wrap'>
  17. <!-- 第一副画面 -->
  18. <li>
  19. <!-- 背景 -->
  20. <div class="a_background">
  21. <div class="a_background_top"></div>
  22. <div class="a_background_middle"></div>
  23. <div class="a_background_botton"></div>
  24. </div>
  25. <!-- 云 -->
  26. <div class="cloudArea">
  27. <div class="cloud"></div>
  28. <div class="cloud"></div>
  29. </div>
  30. <!-- 太阳 -->
  31. <div id="sun"></div>
  32. </li>
  33. <!-- 第二副画面 -->
  34. <li>
  35. <!-- 背景图 -->
  36. <div class="b_background"></div>
  37. <div class="b_background_preload"></div>
  38. <!-- 商店 -->
  39. <div class="shop">
  40. <div class="door">
  41. <div class="door-left"></div>
  42. <div class="door-right"></div>
  43. </div>
  44. <!-- 灯 -->
  45. <div class="lamp"></div>
  46. </div>
  47. </li>
  48. <li> 页面3 </li>
  49. </ul>
  50. <div id="boy" class="charector"></div>
  51. <div class="button">
  52. <button>开始</button>
  53. </div>
  54. </div>
  55. </body>
  56. <script type="text/javascript">
  57. $(function() {
  58.  
  59. var container = $("#content");
  60. var swipe = Swipe(container);
  61. // 页面滚动到指定的位置
  62. function scrollTo(time, proportionX) {
  63. var distX = container.width() * proportionX;
  64. swipe.scrollTo(distX, time);
  65. }
  66.  
  67. ////////////////////////////////////////////////////////
  68. // ================= 动画处理 ======================= //
  69. ////////////////////////////////////////////////////////
  70.  
  71. // 用来临时调整页面
  72. swipe.scrollTo(container.width(), 0);
  73.  
  74. var boy = BoyWalk();
  75.  
  76. function startRun() {
  77.  
  78. boy.walkTo(2000, 0.5)
  79. .then(function() {
  80. //暂停走路
  81. boy.stopWalk()
  82. })
  83. .then(function() {
  84. //开门
  85. return openDoor();
  86. })
  87. .then(function() {
  88. //开灯
  89. lamp.bright()
  90. })
  91. .then(function() {
  92. //进商店
  93. return boy.toShop(2000)
  94. }).then(function() {
  95. //出商店
  96. return boy.outShop(2000)
  97. }).then(function() {
  98. //灯暗
  99. lamp.dark()
  100. });
  101. }
  102.  
  103.  
  104. // 开始
  105. $("button:first").click(startRun);
  106.  
  107. })
  108. </script>
  109. <script type="text/javascript" src="Swipe.js"></script>
  110. <script type="text/javascript" src="Qixi.js"></script>
  111.  
  112. </html>
  1. ///////////
  2. //灯动画 //
  3. ///////////
  4. var lamp = {
  5. elem: $('.b_background'),
  6. bright: function() {
  7. this.elem.addClass('lamp-bright');
  8. },
  9. dark: function() {
  10. this.elem.removeClass('lamp-bright');
  11. }
  12. };
  13.  
  14.  
  15. function doorAction(left, right, time) {
  16. var $door = $('.door');
  17. var doorLeft = $('.door-left');
  18. var doorRight = $('.door-right');
  19. var defer = $.Deferred();
  20. var count = 2;
  21. // 等待开门完成
  22. var complete = function() {
  23. if (count == 1) {
  24. defer.resolve();
  25. return;
  26. }
  27. count--;
  28. };
  29. doorLeft.transition({
  30. 'left': left
  31. }, time, complete);
  32. doorRight.transition({
  33. 'left': right
  34. }, time, complete);
  35. return defer;
  36. }
  37.  
  38. // 开门
  39. function openDoor() {
  40. return doorAction('-50%', '100%', 2000);
  41. }
  42.  
  43. // 关门
  44. function shutDoor() {
  45. return doorAction('0%', '50%', 2000);
  46. }
  47.  
  48.  
  49. var instanceX;
  50.  
  51. /**
  52.   * 小孩走路
  53.   * @param {[type]} container [description]
  54.   */
  55. function BoyWalk() {
  56.  
  57. var container = $("#content");
  58. // 页面可视区域
  59. var visualWidth = container.width();
  60. var visualHeight = container.height();
  61.  
  62. // 获取数据
  63. var getValue = function(className) {
  64. var $elem = $('' + className + '');
  65. // 走路的路线坐标
  66. return {
  67. height: $elem.height(),
  68. top: $elem.position().top
  69. };
  70. }
  71. // 路的Y轴
  72. var pathY = function() {
  73. var data = getValue('.a_background_middle');
  74. return data.top + data.height / 2;
  75. }();
  76.  
  77. var $boy = $("#boy");
  78. var boyWidth = $boy.width();
  79. var boyHeight = $boy.height();
  80.  
  81. // 设置下高度
  82. $boy.css({
  83. top: pathY - boyHeight + 25
  84. })
  85.  
  86. // 暂停走路
  87. function pauseWalk() {
  88. $boy.addClass('pauseWalk');
  89. }
  90.  
  91. // 恢复走路
  92. function restoreWalk() {
  93. $boy.removeClass('pauseWalk');
  94. }
  95.  
  96. // css3的动作变化
  97. function slowWalk() {
  98. $boy.addClass('slowWalk');
  99. }
  100.  
  101. // 用transition做运动
  102. function stratRun(options, runTime) {
  103. var dfdPlay = $.Deferred();
  104. // 恢复走路
  105. restoreWalk();
  106. // 运动的属性
  107. $boy.transition(
  108. options,
  109. runTime,
  110. 'linear',
  111. function() {
  112. dfdPlay.resolve(); // 动画完成
  113. });
  114. return dfdPlay;
  115. }
  116.  
  117. // 开始走路
  118. function walkRun(time, dist, disY) {
  119. time = time || 3000;
  120. // 脚动作
  121. slowWalk();
  122. // 开始走路
  123. var d1 = stratRun({
  124. 'left': dist + 'px',
  125. 'top': disY ? disY : undefined
  126. }, time);
  127. return d1;
  128. }
  129.  
  130.  
  131. // 走进商店
  132. function walkToShop(runTime) {
  133. var defer = $.Deferred();
  134. var doorObj = $('.door')
  135. // 门的坐标
  136. var offsetDoor = doorObj.offset();
  137. var doorOffsetLeft = offsetDoor.left;
  138. // 小孩当前的坐标
  139. var offsetBoy = $boy.offset();
  140. var boyOffetLeft = offsetBoy.left;
  141.  
  142. // 当前需要移动的坐标
  143. instanceX = (doorOffsetLeft + doorObj.width() / 2) - (boyOffetLeft + $boy.width() / 2);
  144.  
  145. // 开始走路
  146. var walkPlay = stratRun({
  147. transform: 'translateX(' + instanceX + 'px),scale(0.3,0.3)',
  148. opacity: 0.1
  149. }, 2000);
  150. // 走路完毕
  151. walkPlay.done(function() {
  152. $boy.css({
  153. opacity: 0
  154. })
  155. defer.resolve();
  156. })
  157. return defer;
  158. }
  159.  
  160. // 走出店
  161. function walkOutShop(runTime) {
  162. // ?
  163. }
  164.  
  165.  
  166. // 计算移动距离
  167. function calculateDist(direction, proportion) {
  168. return (direction == "x" ?
  169. visualWidth : visualHeight) * proportion;
  170. }
  171.  
  172. return {
  173. // 开始走路
  174. walkTo: function(time, proportionX, proportionY) {
  175. var distX = calculateDist('x', proportionX)
  176. var distY = calculateDist('y', proportionY)
  177. return walkRun(time, distX, distY);
  178. },
  179. // 走进商店
  180. toShop: function() {
  181. return walkToShop.apply(null, arguments);
  182. },
  183. // 走出商店
  184. outShop: function() {
  185. return walkOutShop.apply(null, arguments);
  186. },
  187. // 停止走路
  188. stopWalk: function() {
  189. pauseWalk();
  190. },
  191. setColoer: function(value) {
  192. $boy.css('background-color', value)
  193. }
  194. }
  195. }
  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.  
  72. .pauseWalk {
  73. -webkit-animation-play-state: paused;
  74. }
  75.  
  76. /*普通慢走*/
  77.  
  78. @-webkit-keyframes person-slow {
  79. 0% {
  80. background-position: -0px -291px;
  81. }
  82. 25% {
  83. background-position: -602px -0px;
  84. }
  85. 50% {
  86. background-position: -302px -291px;
  87. }
  88. 75% {
  89. background-position: -151px -291px;
  90. }
  91. 100% {
  92. background-position: -0px -291px;
  93. }
  94. }
  95.  
  96. @-moz-keyframes person-slow {
  97. 0% {
  98. background-position: -0px -291px;
  99. }
  100. 25% {
  101. background-position: -602px -0px;
  102. }
  103. 50% {
  104. background-position: -302px -291px;
  105. }
  106. 75% {
  107. background-position: -151px -291px;
  108. }
  109. 100% {
  110. background-position: -0px -291px;
  111. }
  112. }
  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. }
下一节