Advertisement
nicolaslagios

Untitled

Jan 26th, 2023 (edited)
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var images = [
  2.     "https://cavofregata.econtentsys.gr/1.jpg",
  3.     "https://cavofregata.econtentsys.gr/2.jpg",
  4.     "https://cavofregata.econtentsys.gr/3.jpg",
  5. ];
  6.  
  7. const image = document.createElement("img");
  8. const villaeffect = document.getElementsByClassName("villaeffect");
  9. image.src = images[0];
  10. image.style.position = "fixed";
  11. document.body.appendChild(image);
  12.  
  13. let x = 0;
  14. let y = 0;
  15. let targetX = 0;
  16. let targetY = 0;
  17. let easing = 0.1;
  18. let lastX = 0;
  19. let rotateValue = 0;
  20. let damping = 0.99;
  21.  
  22. document.addEventListener("mousemove", (event) => {
  23.     targetX = event.clientX;
  24.     targetY = event.clientY;
  25.     let deltaX = event.clientX - lastX;
  26.     rotateValue += deltaX * 0.01;
  27.     lastX = event.clientX;
  28. });
  29.  
  30. function animate() {
  31.     x += (targetX - x) * easing;
  32.     y += (targetY - y) * easing;
  33.     if (Math.abs(rotateValue) < 0.05) {
  34.         rotateValue = 0;
  35.     } else {
  36.         rotateValue *= damping;
  37.     }
  38.     image.style.left = x + "px";
  39.     image.style.top = y + "px";
  40.     image.style.transform = `rotate(${rotateValue}deg)`;
  41.     requestAnimationFrame(animate);
  42. }
  43.  
  44. animate();
  45.  
  46. for (let i = 0; i < villaeffect.length; i++) {
  47.     //villaeffect[i].addEventListener("mouseover", () => {
  48.     villaeffect[i].addEventListener("mouseenter", () => {
  49.         currentImageIndex = i;
  50.         showImage();
  51.     });
  52.     //villaeffect[i].addEventListener("mouseout", hideImage);
  53.     villaeffect[i].addEventListener("mouseleave", hideImage);
  54. }
  55. function showImage() {
  56.     image.src = images[currentImageIndex];
  57.     image.style.visibility = "visible";
  58. }
  59. function hideImage() {
  60.     image.style.visibility = "hidden";
  61. }
  62.  
  63. let text;
  64. let textCreated = false;
  65. let interval;
  66. for (let i = 0; i < villaeffect.length; i++) {
  67.     villaeffect[i].addEventListener("mouseover", () => {
  68.         currentImageIndex = i;
  69.         showImage();
  70.         // create the text element only if it hasn't been created yet
  71.         if (!textCreated) {
  72.             text = document.createElement("div");
  73.             text.innerHTML = "";
  74.             text.style.cssText = "font-size:25px; position:absolute;";
  75.             villaeffect[i].appendChild(text);
  76.             textCreated = true;
  77.         }
  78.         // typewriting effect
  79.         clearInterval(interval);
  80.         let index = 0;
  81.         interval = setInterval(() => {
  82.             text.innerHTML += `0${i+1}`[index];
  83.             index++;
  84.             if (index === `0${i+1}`.length) {
  85.                 clearInterval(interval);
  86.             }
  87.         }, 50);
  88.     });
  89.     villaeffect[i].addEventListener("mouseout", () => {
  90.         hideImage();
  91.         villaeffect[i].removeChild(text);
  92.         textCreated = false;
  93.         clearInterval(interval);
  94.     });
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement