资讯专栏INFORMATION COLUMN

表白桃心动画前端JS

liangzai_cool / 2176人阅读

摘要:文件心心各小方块相对自身中心的位置自身中心确定为的方块各方块相对爱心中心的位置爱心中心

biu.html文件

</>复制代码

  1. biu心心
  2. Version: 0.0.2
  3. Time: 2019/4/17
  4. © csyr

love.js

</>复制代码

  1. const blk_pitn = { //各小方块相对【自身中心】的位置 -- 【自身中心】确定为#div22的方块
  2. block1: [[0, 1], [0, 0], [-1, 0], [-1, -1]],
  3. block2: [[0, 1], [0, 0], [-1, 0], [0, -1]],
  4. block3: [[-1, 1], [0, 0], [-1, 0], [-1, -1]],
  5. block4: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
  6. block5: [[-1, 1], [0, 0], [-1, 0], [0, -1]],
  7. block6: [[0, -1], [0, 0], [-1, 0], [1, -1]],
  8. block7: [[-1, -1], [0, 0], [-1, 0], [1, 0]],
  9. block8: [[-1, 1], [0, 0], [-1, 0], [-1, -1]], /* 3 */
  10. block9: [[0, -1], [0, 0], [-1, 0], [1, 0]],
  11. block10: [[-1, 1], [0, 0], [-1, 0], [1, 0]],
  12. block11: [[2, 0], [0, 0], [-1, 0], [1, 0]], /* — */
  13. block12: [[0, 1], [0, 0], [-1, 0], [0, -1]], /* 2 */
  14. block13: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
  15. block14: [[1, 1], [0, 0], [-1, 0], [1, 0]],
  16. block15: [[1, -1], [0, 0], [-1, 0], [1, 0]],
  17. block16: [[-1, -1], [0, 0], [-1, 0], [1, 0]], /* 7 */
  18. block17: [[0, 1], [0, 0], [-1, 0], [0, -1]], /* 2 */
  19. block18: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
  20. block19: [[0, -1], [0, 0], [-1, 0], [1, 0]], /* 9 */
  21. block20: [[1, -1], [0, 0], [-1, 0], [1, 0]],
  22. block21: [[0, 1], [0, 0], [-1, 0], [-1, -1]], /* 1 */
  23. block22: [[1, 1], [0, 0], [-1, 0], [1, 0]], /* 14 */
  24. block23: [[0, 2], [0, 0], [0, -1], [0, 1]] /* | */
  25. },
  26. offset_pitn = { //各方块block相对【爱心中心】的位置
  27. block1: [5, 3],
  28. block2: [5, 1],
  29. block3: [3, 4],
  30. block4: [3, 2],
  31. block5: [3, -1],
  32. block6: [2, 5],
  33. block7: [2, 1],
  34. block8: [1, -1],
  35. block9: [1, -3],
  36. block10: [1, 2],
  37. block11: [0, 3],
  38. block12: [0, 0], /* 【爱心中心】*/
  39. block13: [-1, -4],
  40. block14: [0, -2],
  41. block15: [-2, 4],
  42. block16: [-2, 2],
  43. block17: [-2, 0],
  44. block18: [-3, -2],
  45. block19: [-4, 0],
  46. block20: [-3, 5],
  47. block21: [-5, 3],
  48. block22: [-4, 1],
  49. block23: [-6, 1] /* 因动画需要移动一个方块,故y轴坐标-1*/
  50. };
  51. let blocks = document.getElementsByClassName("block"),
  52. block = blocks[0],
  53. love = document.getElementsByClassName("love")[0],
  54. timer = null,
  55. index = 0, //记录拼接爱心的动画步骤
  56. clone_block; //用于克隆方块
  57. //1.移动方块的【自身中心】到【爱心中心】
  58. block.style.top = "50%";
  59. block.style.left = "50%";
  60. block.style.margin = "-20px 0 0 -20px";
  61. const block_left = parseFloat(window.getComputedStyle(block, null).left.slice(0, -2)), //【爱心中心】 左边距离父元素的距离
  62. block_top = parseFloat(window.getComputedStyle(block, null).top.slice(0, -2)); //【爱心中心】 顶部距离父元素的距离
  63. function Next() {
  64. if (++index >= 24) {
  65. clearInterval(timer);
  66. Rise();
  67. // alert("已经是最后一个了!");
  68. return;
  69. }
  70. block.style.visibility = "visible"; //升空动画前允许可见
  71. //2.移动方块到指定的位置-即是移动【自身中心】到目标位置
  72. block.style.left = block_left + 40 * offset_pitn["block" + index][0] + "px";
  73. block.style.top = block_top - 40 * offset_pitn["block" + index][1] + "px";
  74. for (let i = 0; i < block.children.length; i++) {
  75. // block.children[1].innerText = index; //编号便于调试
  76. block.children[i].style.left = blk_pitn["block" + index][i][0] * -40 + "px";
  77. /* -40 是因为逻辑坐标和浏览器的x,y轴方向不一样*/
  78. block.children[i].style.top = blk_pitn["block" + index][i][1] * -40 + "px";
  79. }
  80. //3.克隆方块—保存现在的位置
  81. /* 一共会克隆23个方块,加上原先的一个方块block,共24个方块,即多出原先的block方块*/
  82. clone_block = block.cloneNode(true);
  83. love.appendChild(clone_block);
  84. if (love.children.length >= 24) {
  85. blocks[blocks.length - 1].children[2].style.display = "none"; //去掉多余的小方块
  86. block.style.display = "none"; //隐藏多出的block方块
  87. }
  88. }
  89. function Rise() {
  90. //4.爱心升高,多出的那个小方块开始掉落
  91. console.log("开始升空");
  92. let timer2 = null,
  93. distance = 0;
  94. /* 升高时,移动的距离*/
  95. const target = 120, /* 目标距离*/
  96. speed = 1;
  97. /*移动速度*/
  98. let love_top = parseFloat(window.getComputedStyle(love, null).top.slice(0, -2)); //爱心盒子距离屏幕顶部的距离
  99. timer2 = setInterval(() => {
  100. distance += speed;
  101. // console.log(distance);
  102. if (distance >= target) {
  103. clearInterval(timer2);
  104. console.log("升空完毕");
  105. }
  106. love.style.top = (love_top - distance) + "px";
  107. }, 22);
  108. }
  109. window.onload = function () {
  110. setTimeout(() => {
  111. timer = setInterval(() => {
  112. Next();
  113. }, 300);
  114. }, 12000); //gif图播放完毕所需时间为11.73s
  115. };


love.css文件

</>复制代码

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. border: 0;
  5. }
  6. .icon-love {
  7. width: 400px;
  8. }
  9. html, body {
  10. width: 100%;
  11. height: 100%;
  12. }
  13. body {
  14. /*background-color: skyblue;*/
  15. overflow: hidden; /*隐藏超出的部分*/
  16. }
  17. .container {
  18. width: 100%;
  19. height: 100%;
  20. position: relative;
  21. }
  22. /*---------------------- body_left -------------------------*/
  23. .body_left {
  24. width: 300px;
  25. height: 300px;
  26. left: 0;
  27. bottom: 110px;
  28. position: absolute;
  29. z-index: 98;
  30. }
  31. /*---------------------- body_left -------------------------*/
  32. /*---------------------- body_center -------------------------*/
  33. .container .love {
  34. width: 520px; /* 13 * 40 */
  35. height: 440px; /* 11 * 40 */
  36. left: 50%;
  37. top: 50%;
  38. position: absolute;
  39. margin: -260px 0 0 -220px;
  40. /*background-color: gray;*/
  41. }
  42. .love .block {
  43. right: 0;
  44. position: absolute;
  45. visibility: hidden; /*未开始升空动画前隐藏*/
  46. background-color: yellow;
  47. }
  48. .love .block div {
  49. width: 40px;
  50. height: 40px;
  51. position: absolute;
  52. background: url("../images/heart.png") no-repeat;
  53. background-size: contain;
  54. /*background-color: #c40908;*/
  55. /*border: 1px solid silver;*/
  56. box-sizing: border-box;
  57. }
  58. /*---------------------- body_center -------------------------*/
  59. /*---------------------- footer -------------------------*/
  60. @keyframes border {
  61. 0% {
  62. width: 0;
  63. }
  64. 5% {
  65. width: 5%;
  66. }
  67. 10% {
  68. width: 10%;
  69. }
  70. 15% {
  71. width: 15%;
  72. }
  73. 20% {
  74. width: 20%;
  75. }
  76. 25% {
  77. width: 25%;
  78. }
  79. 30% {
  80. width: 30%;
  81. }
  82. 35% {
  83. width: 35%;
  84. }
  85. 40% {
  86. width: 40%;
  87. }
  88. 45% {
  89. width: 45%;
  90. }
  91. 50% {
  92. width: 50%;
  93. }
  94. 55% {
  95. width: 55%;
  96. }
  97. 60% {
  98. width: 60%;
  99. }
  100. 65% {
  101. width: 65%;
  102. }
  103. 70% {
  104. width: 70%;
  105. }
  106. 75% {
  107. width: 75%;
  108. }
  109. 80% {
  110. width: 80%;
  111. }
  112. 85% {
  113. width: 85%;
  114. }
  115. 90% {
  116. width: 90%;
  117. }
  118. 95% {
  119. width: 95%;
  120. }
  121. 100% {
  122. width: 100%;
  123. }
  124. }
  125. .footer {
  126. bottom: 30px;
  127. position: relative;
  128. z-index: 99;
  129. }
  130. .footer .border .border-top {
  131. /*width: 0;*/
  132. /*display: inline-block;*/
  133. border-top: 3px solid black;
  134. transform-origin: left center;
  135. -webkit-animation: border 312 linear;
  136. -o-animation: border 12s linear;
  137. animation: border 12s linear;
  138. animation-fill-mode : both;
  139. /*border-bottom: none;*/
  140. }
  141. .footer .border .border-bottom {
  142. /*width: 0;*/
  143. /*display: inline-block;*/
  144. float: right;
  145. border-top: 3px solid red;
  146. transform-origin: right center;
  147. -webkit-animation: border 7s linear 12s;
  148. -o-animation: border 7s linear 12s;
  149. animation: border 7s linear 12s;
  150. animation-fill-mode : both;
  151. /*border-bottom: none;*/
  152. }
  153. .footer .copyright {
  154. width: 100%;
  155. height: 30px;
  156. position: absolute;
  157. bottom: -30px;
  158. text-align: center;
  159. /*background-color: gray;*/
  160. }
  161. .copyright div {
  162. width: 30%;
  163. line-height: 30px;
  164. display: inline-block;
  165. }
  166. .copyright div span {
  167. color: dimgray;
  168. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/104463.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<