Wedding Invitation
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

app.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import './bootstrap';
  2. //import 'resources/js/app.js';
  3. import '@fortawesome/fontawesome-free/css/all.min.css';
  4. document.addEventListener('DOMContentLoaded', function () {
  5. imageLoading()
  6. reset() // reset everrything to ensure smooth
  7. sessionStorage.setItem("initial-load", "true"); // will be used by click event
  8. });
  9. document.addEventListener('click', function () {
  10. let initLoad = sessionStorage.getItem("initial-load")
  11. // parts is intro cover
  12. let parts = document.getElementsByClassName("part");
  13. for (let part of parts) {
  14. part.classList.add("shadow-lg")
  15. }
  16. let body = document.querySelector("body");
  17. body.classList.remove("no-scroll");
  18. // the intro - only done once and
  19. if (initLoad == "true") {
  20. playMusic();
  21. sessionStorage.removeItem("initial-load")
  22. // delay timer to have that umpph feels my G
  23. setTimeout(() => {
  24. // smooth scroll only happen for first render
  25. smoothScrollBy(100, 3500);
  26. }, 600)
  27. }
  28. setTimeout(()=>{
  29. eventCountdown();
  30. },2000)
  31. });
  32. function smoothScrollBy(distance, duration) {
  33. const start = window.pageYOffset;
  34. const startTime = performance.now();
  35. function scroll() {
  36. const now = performance.now();
  37. const time = Math.min(1, (now - startTime) / duration);
  38. const timeFunction = time * (2 - time); // Ease out effect
  39. window.scrollTo(0, start + timeFunction * distance);
  40. if (time < 1) {
  41. requestAnimationFrame(scroll);
  42. }
  43. }
  44. requestAnimationFrame(scroll);
  45. }
  46. // to ensure all images is loaded before starting animation and gimmick
  47. function imageLoading() {
  48. const images = document.querySelectorAll("img");
  49. let loadedCount = 0;
  50. const totalImages = images.length;
  51. images.forEach(image => {
  52. if (image.complete) {
  53. loadedCount++;
  54. } else {
  55. image.addEventListener("load", () => {
  56. loadedCount++;
  57. checkAllImagesLoaded();
  58. });
  59. image.addEventListener("error", () => {
  60. loadedCount++;
  61. checkAllImagesLoaded();
  62. });
  63. }
  64. });
  65. function checkAllImagesLoaded() {
  66. if (loadedCount === totalImages) {
  67. let loading = document.getElementById("loading");
  68. loading.remove();
  69. // parts is intro cover
  70. let parts = document.getElementsByClassName("part");
  71. for (let part of parts) {
  72. part.classList.remove("hidden")
  73. }
  74. }
  75. }
  76. //somehow the server work so fast, script doesn't have much time to set the load eventlistener
  77. //thus we check here, to ensure checkAllImagesLoaded run
  78. checkAllImagesLoaded();
  79. }
  80. function reset() {
  81. // reset to top
  82. let body = document.querySelector("body");
  83. body.classList.remove("no-scroll");
  84. window.scrollTo(0, 0);
  85. setTimeout(() => {
  86. body.classList.add("no-scroll");
  87. }, 400)
  88. }
  89. function playMusic(){
  90. var audioElement = document.getElementById('player');
  91. var hasPlayed = false;
  92. if (!hasPlayed) {
  93. console.log('play')
  94. var playPromise = audioElement.play();
  95. if (playPromise !== undefined) {
  96. playPromise.then(function() {
  97. // Automatic playback started!
  98. hasPlayed = true; // Prevent further attempts to play
  99. }).catch(function(error) {
  100. // Automatic playback failed.
  101. console.log('Autoplay failed: ' + error);
  102. });
  103. }
  104. }
  105. }
  106. function eventCountdown(){
  107. //Countdown date (adjust this date to your desired countdown target)
  108. const countdownDate = new Date('2024-08-17T11:00:00').getTime();
  109. //Update the countdown every second
  110. const countdownElement = document.getElementById('countdown');
  111. const countdownTimer = setInterval(() => {
  112. //Get the current date and time
  113. const now = new Date().getTime();
  114. //Calculate the time remaining
  115. const distance = countdownDate - now;
  116. //Calculate days, hours, minutes, and seconds
  117. const days = Math.floor(distance / (1000 * 60 * 60 * 24));
  118. const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  119. const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  120. const seconds = Math.floor((distance % (1000 * 60)) / 1000);
  121. //Format the countdown into a string
  122. const countdownHTML = `
  123. <div class="flex flex-col">
  124. <div class="flex gap-8 justify-center">
  125. <div class="font-medium text-gray-500">${days}</div>
  126. <div class="font-medium text-gray-500">:</div>
  127. <div class="font-medium text-gray-500">${hours.toString().padStart(2, '0')}</div>
  128. <div class="font-medium text-gray-500">:</div>
  129. <div class="font-medium text-gray-500">${minutes.toString().padStart(2, '0')}</div>
  130. <div class="font-medium text-gray-500">:</div>
  131. <div class="font-medium text-gray-500">${seconds.toString().padStart(2, '0')}</div>
  132. </div>
  133. <div class="flex gap-14 justify-center">
  134. <div class="font-medium text-gray-600">Hari</div>
  135. <div class="font-medium text-gray-600">Jam</div>
  136. <div class="font-medium text-gray-600">Minit</div>
  137. <div class="font-medium text-gray-600">Saat</div>
  138. </div>
  139. </div>
  140. `;
  141. //Display the countdown in the element
  142. countdownElement.innerHTML = countdownHTML;
  143. //If the countdown is over, clear the timer and display a message
  144. if (distance < 0) {
  145. clearInterval(countdownTimer);
  146. countdownElement.innerHTML = 'Majlis berlangsung';
  147. }
  148. }, 1000); // Update every second (1000 milliseconds)
  149. }