Advertisement
oscarviedma

Funcionalidad JavaScript OV Dock - OV DIVI

Mar 12th, 2024 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. <script src="https://cdn.jsdelivr.net/npm/scrollbooster@3.0.2/dist/scrollbooster.min.js"></script>
  2.  
  3. <script>
  4. // Seleccionar elementos
  5. const dockContainer = document.querySelector(".ov-dock");
  6. const dockItems = dockContainer.querySelectorAll(".ov-dock-item");
  7.  
  8. // Constantes
  9. const defaultItemScale = 1;
  10. const desktopHoverItemScale = 1.8;
  11. const desktopNeighborItemScale = 1.4;
  12. const desktopDefaultMargin = "10px";
  13. const desktopExpandedMargin = "24px";
  14. const mobileHoverItemScale = 1.4;
  15. const mobileNeighborItemScale = 1.2;
  16. const mobileDefaultMargin = "5px";
  17. const mobileExpandedMargin = "12px";
  18.  
  19. let hoverItemScale, neighborItemScale, defaultMargin, expandedMargin;
  20. let scrollBooster;
  21.  
  22. const updateConstants = () => {
  23. const isMobile = window.innerWidth <= 768;
  24.  
  25. if (isMobile) {
  26. hoverItemScale = mobileHoverItemScale;
  27. neighborItemScale = mobileNeighborItemScale;
  28. defaultMargin = mobileDefaultMargin;
  29. expandedMargin = mobileExpandedMargin;
  30. } else {
  31. hoverItemScale = desktopHoverItemScale;
  32. neighborItemScale = desktopNeighborItemScale;
  33. defaultMargin = desktopDefaultMargin;
  34. expandedMargin = desktopExpandedMargin;
  35. }
  36. };
  37.  
  38. const updateDockItems = () => {
  39. dockItems.forEach((item, index) => {
  40. let scale = defaultItemScale;
  41. let margin = defaultMargin;
  42.  
  43. if (item.isHovered) {
  44. scale = hoverItemScale;
  45. margin = expandedMargin;
  46. } else if (item.isNeighbor) {
  47. scale = neighborItemScale;
  48. margin = expandedMargin;
  49. }
  50.  
  51. item.style.transform = `scale(${scale})`;
  52. item.style.margin = `0 ${margin}`;
  53. });
  54. };
  55.  
  56. // Evento de hover/toque
  57. dockItems.forEach((item) => {
  58. const handleHover = () => {
  59. dockItems.forEach((otherItem) => {
  60. otherItem.isHovered = otherItem === item;
  61. otherItem.isNeighbor =
  62. Math.abs(
  63. Array.from(dockItems).indexOf(otherItem) -
  64. Array.from(dockItems).indexOf(item)
  65. ) === 1;
  66. });
  67.  
  68. updateDockItems();
  69. };
  70.  
  71. item.addEventListener("mouseenter", handleHover);
  72. item.addEventListener("touchstart", handleHover, { passive: true });
  73. });
  74.  
  75. dockContainer.addEventListener("mouseleave", () => {
  76. dockItems.forEach((item) => {
  77. item.isHovered = item.isNeighbor = false;
  78. });
  79.  
  80. updateDockItems();
  81. });
  82.  
  83. const initScrollBooster = () => {
  84. if (scrollBooster) {
  85. scrollBooster.destroy();
  86. }
  87.  
  88. if (window.innerWidth <= 768) {
  89. scrollBooster = new ScrollBooster({
  90. viewport: document.querySelector(".viewport-dock"),
  91. content: document.querySelector(".ov-dock"),
  92. scrollMode: "transform",
  93. direction: "horizontal",
  94. });
  95. }
  96. };
  97.  
  98. const handleResize = () => {
  99. updateConstants();
  100. initScrollBooster();
  101. };
  102.  
  103. // Inicializar ScrollBooster y manejar redimensionamiento
  104. if (document.readyState !== "loading") {
  105. handleResize();
  106. } else {
  107. document.addEventListener("DOMContentLoaded", handleResize);
  108. }
  109.  
  110. // Actualizar constantes al cambiar el tamaño de la ventana
  111. window.addEventListener("resize", handleResize);
  112. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement