Advertisement
oscarviedma

Codigo funcionalidad GSAP cursor hover divi

Jul 28th, 2023 (edited)
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. <!-- Sincronizar librería GSAP -->
  2. <script src='https://unpkg.co/gsap/dist/gsap.min.js'></script>
  3.  
  4. <script>
  5. // Insertar el atributo data-image con las urls de las imagenes a cada fila
  6. const addData = document.querySelectorAll('.ov-data-image');
  7. const enlacesIMG = [
  8. '#',
  9. '#',
  10. '#',
  11. '#',
  12. '#',
  13. ];
  14.  
  15. addData.forEach((div, index) => {
  16. div.setAttribute('data-img', enlacesIMG[index]);
  17. });
  18. </script>
  19.  
  20. <script>
  21. // Funcionalidad GSAP efecto hover image
  22. const allVenues = gsap.utils.toArray(".cursor-img-item");
  23. const venueImageWrap = document.querySelector(".cursor-img-wrap");
  24. const venueImage = document.querySelector(".cursor-img");
  25.  
  26. function initVenues() {
  27. allVenues.forEach((link) => {
  28. link.addEventListener("mouseenter", venueHover);
  29. link.addEventListener("mouseleave", venueHover);
  30. link.addEventListener("mousemove", moveVenueImage);
  31. });
  32. }
  33.  
  34. function moveVenueImage(e) {
  35. let xpos = e.clientX;
  36. let ypos = e.clientY;
  37. const tl = gsap.timeline();
  38. tl.to(venueImageWrap, {
  39. x: xpos,
  40. y: ypos
  41. });
  42. }
  43.  
  44. function venueHover(e) {
  45. if (e.type === "mouseenter") {
  46. const targetImage = e.target.dataset.img;
  47.  
  48. const t1 = gsap.timeline();
  49. t1.set(venueImage, {
  50. backgroundImage: `url(${targetImage})`
  51. }).to(venueImageWrap, {
  52. duration: 0.5,
  53. autoAlpha: 1
  54. });
  55. } else if (e.type === "mouseleave") {
  56. const tl = gsap.timeline();
  57. tl.to(venueImageWrap, {
  58. duration: 0.5,
  59. autoAlpha: 0
  60. });
  61. }
  62. }
  63.  
  64. function init() {
  65. initVenues();
  66. }
  67.  
  68. window.addEventListener("load", function () {
  69. init();
  70. });
  71. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement