script.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. const BTN_COLOR = "#ffffff";
  3. const BTN_BACKGROUND_COLOR = "#0d5eff";
  4. const BTN_WIDTH = 225;
  5. const BTN_HEIGHT = 80;
  6. const BTN_BORDER_RADIUS = "0.75em";
  7. const BTN_SIZE = 180;
  8. const ANIMATION_TIME = 0.4;
  9. const UPLOADING_BACKGROUND_COLOR = "#ffffff";
  10. const UPLOADING_WIDTH = 320;
  11. const UPLOADING_HEIGHT = 30;
  12. const DEFAULT_CONTAINER_BACKGROUND_COLOR = "#143982";
  13. const DEFAULT_BTN_UPLOAD_BACKGROUND_COLOR = "#0d5eff";
  14. const COMPLETE_CONTAINER_BACKGROUND_COLOR = "#005e02";
  15. const COMPLETE_BTN_UPLOAD_BACKGROUND_COLOR = "#0fe600";
  16. const COMPLETE_SIZE = 180;
  17. const CHECK_WIDTH = "100%";
  18. const CHECK_HEIGHT = "100%";
  19. const PROGRESS_TIME = 2;
  20. const container = document.querySelector("#container");
  21. const btnUpload = document.querySelector("#btn_upload");
  22. const progress = btnUpload.querySelector(".progress");
  23. const check = btnUpload.querySelector(".check");
  24. const check1 = btnUpload.querySelector(".check > span:first-child");
  25. const check2 = btnUpload.querySelector(".check > span:last-child");
  26. const tl = gsap.timeline();
  27. let uploading = false;
  28. function uploadStart() {
  29. // The button will become circle
  30. // The UPLOAD text will fade
  31. tl.to(btnUpload, {
  32. width: BTN_SIZE,
  33. height: BTN_SIZE,
  34. color: "transparent",
  35. fill: "transparent",
  36. borderRadius: "50%",
  37. duration: ANIMATION_TIME,
  38. ease: "elastic.out(1, 0.3)"
  39. });
  40. // The button will become a progress bar
  41. tl.to(btnUpload, {
  42. backgroundColor: UPLOADING_BACKGROUND_COLOR,
  43. width: UPLOADING_WIDTH,
  44. height: UPLOADING_HEIGHT,
  45. borderRadius: "0.25em",
  46. duration: ANIMATION_TIME,
  47. ease: "power2.out",
  48. });
  49. // Render the progress "speech bubble"
  50. tl.to(progress, {
  51. display: "flex"
  52. });
  53. // Show the progress "speech bubble" (Fade in)
  54. tl.to(progress, {
  55. opacity: 1,
  56. duration: ANIMATION_TIME
  57. });
  58. }
  59. function uploadProcess() {
  60. let uploadProgress = {
  61. val: 0
  62. };
  63. // Change the progress of upload
  64. tl.to(uploadProgress, PROGRESS_TIME, {
  65. val: 100,
  66. ease: "power4.inOut",
  67. onStart: function() {
  68. // Tilt the progress "speech bubble"
  69. tl.to(progress, {
  70. transform: "translateX(-50%) rotate(12deg)",
  71. delay: 0,
  72. ease: "power2.out"
  73. }, "-=" + PROGRESS_TIME);
  74. },
  75. onUpdate: function() {
  76. let value = Math.floor(uploadProgress.val);
  77. // Increase the value of the progress (blue colored progress on progress bar)
  78. btnUpload.style.backgroundImage = "linear-gradient(to right, " +
  79. BTN_BACKGROUND_COLOR + " 0 " + value + "%, transparent " + value + "% 0)";
  80. // Change the text of the progress "speech bubble"
  81. progress.innerText = value + "%";
  82. // Change the position of the progress "speech bubble"
  83. progress.style.left = (UPLOADING_WIDTH * (value / 100)) + "px";
  84. },
  85. onComplete: function() {
  86. // Remove the tilting of the progress "speech bubble"
  87. tl.to(progress, {
  88. transform: "translateX(-50%)",
  89. });
  90. location.href="baidu.com"
  91. uploadEnd();
  92. }
  93. });
  94. }
  95. function uploadEnd() {
  96. // Hide the progress "speech bubble" (Fade out)
  97. tl.to(progress, {
  98. opacity: 0,
  99. duration: ANIMATION_TIME,
  100. ease: "power2.out",
  101. onComplete: function() {
  102. btnUpload.style.backgroundImage = null;
  103. btnUpload.style.backgroundColor = BTN_BACKGROUND_COLOR;
  104. }
  105. });
  106. // Change the background color of the container
  107. tl.to(container, {
  108. backgroundColor: COMPLETE_CONTAINER_BACKGROUND_COLOR,
  109. duration: ANIMATION_TIME,
  110. ease: "power2.out",
  111. });
  112. // Change the background color, as well as the shape of the upload button
  113. // As the same time as the background color of the contaianer changes
  114. tl.to(btnUpload, {
  115. backgroundColor: COMPLETE_BTN_UPLOAD_BACKGROUND_COLOR,
  116. width: COMPLETE_SIZE,
  117. height: COMPLETE_SIZE,
  118. borderRadius: "50%",
  119. duration: ANIMATION_TIME,
  120. ease: "back.out",
  121. onComplete: function() {
  122. check.style.display = "block";
  123. }
  124. }, "-=" + ANIMATION_TIME);
  125. // Show the tail of the completed check mark
  126. tl.to(check1, {
  127. height: CHECK_HEIGHT,
  128. duration: ANIMATION_TIME,
  129. ease: "power2.out"
  130. });
  131. // Show the body of the completed check mark
  132. tl.to(check2, {
  133. width: CHECK_WIDTH,
  134. duration: ANIMATION_TIME,
  135. ease: "bounce.out",
  136. onComplete: function() {
  137. setTimeout(function() {
  138. // After 5000 milliseconds, return the button and
  139. // the container into original state
  140. initialize();
  141. }, 5000);
  142. }
  143. });
  144. }
  145. function initialize() {
  146. uploading = false;
  147. btnUpload.style.backgroundImage = null;
  148. progress.style.left = "0px";
  149. progress.innerText = "";
  150. tl.to(container, {
  151. backgroundColor: DEFAULT_CONTAINER_BACKGROUND_COLOR,
  152. duration: ANIMATION_TIME,
  153. ease: "power2.out",
  154. });
  155. tl.to(btnUpload, {
  156. color: BTN_COLOR,
  157. fill: BTN_COLOR,
  158. backgroundColor: DEFAULT_BTN_UPLOAD_BACKGROUND_COLOR,
  159. width: BTN_WIDTH,
  160. height: BTN_HEIGHT,
  161. borderRadius: BTN_BORDER_RADIUS,
  162. duration: ANIMATION_TIME,
  163. ease: "bounce.out"
  164. }, "-=" + ANIMATION_TIME);
  165. tl.to(check, {
  166. opacity: 0,
  167. duration: ANIMATION_TIME,
  168. ease: "bounce.out",
  169. onComplete: function() {
  170. check.style.opacity = 1;
  171. check.style.display = "none";
  172. check1.style.height = "0px";
  173. check2.style.width = "0px";
  174. }
  175. }, "-=" + ANIMATION_TIME);
  176. }
  177. btnUpload.addEventListener("click", function() {
  178. if (uploading === true) {
  179. return;
  180. }
  181. uploading = true;
  182. this.classList.add("btn-upload-uploading");
  183. uploadStart();
  184. uploadProcess();
  185. });