Advertisement
oscarviedma

OV Fashion Slider - OV DIVI

May 10th, 2024
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css">
  2. <script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
  3.  
  4. <script>
  5. // Envuelve la sección 'ov-fashion-slider' dentro de 'fashion-slider'
  6. const ovFashionSlider = document.querySelector(".ov-fashion-slider");
  7. const wrapper = document.createElement("div");
  8. wrapper.className = "fashion-slider";
  9. ovFashionSlider.parentNode.insertBefore(wrapper, ovFashionSlider);
  10. wrapper.appendChild(ovFashionSlider);
  11.  
  12. // Definir los colores de las columnas 'swiper-slide'
  13. const colores = ['#9FA051', '#9B89C5', '#D7A594',];
  14.  
  15. const swiperSlides = document.querySelectorAll('.swiper-slide');
  16. swiperSlides.forEach((slide, index) => {
  17. const color = colores[index % colores.length];
  18. slide.setAttribute('data-slide-bg-color', color);
  19. });
  20.  
  21. // Añade a los titulos el atributo 'data-swiper-parallax' con el valor '-130%'
  22. var elementos = document.querySelectorAll('.fashion-slider-title');
  23. elementos.forEach(function(elemento) {
  24. elemento.setAttribute('data-swiper-parallax', '-130%');
  25. });
  26.  
  27. // Insertar las flechas de navegación dentro de 'swiper'
  28. var swiper = document.querySelector('.swiper');
  29.  
  30. var prevButton = document.createElement('div');
  31. prevButton.className = 'fashion-slider-button-prev fashion-slider-button';
  32.  
  33. prevButton.innerHTML = `
  34. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 350 160 90">
  35. <g class="fashion-slider-svg-wrap">
  36. <g class="fashion-slider-svg-circle-wrap">
  37. <circle cx="42" cy="42" r="40"></circle>
  38. </g>
  39. <path class="fashion-slider-svg-arrow" d="M.983,6.929,4.447,3.464.983,0,0,.983,2.482,3.464,0,5.946Z"></path>
  40. <path class="fashion-slider-svg-line" d="M80,0H0"></path>
  41. </g>
  42. </svg>
  43. `;
  44.  
  45. var nextButton = document.createElement('div');
  46. nextButton.className = 'fashion-slider-button-next fashion-slider-button';
  47.  
  48. nextButton.innerHTML = `
  49. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 350 160 90">
  50. <g class="fashion-slider-svg-wrap">
  51. <g class="fashion-slider-svg-circle-wrap">
  52. <circle cx="42" cy="42" r="40"></circle>
  53. </g>
  54. <path class="fashion-slider-svg-arrow" d="M.983,6.929,4.447,3.464.983,0,0,.983,2.482,3.464,0,5.946Z"></path>
  55. <path class="fashion-slider-svg-line" d="M80,0H0"></path>
  56. </g>
  57. </svg>
  58. `;
  59.  
  60. swiper.insertAdjacentElement('beforeend', prevButton);
  61. swiper.insertAdjacentElement('beforeend', nextButton);
  62. </script>
  63.  
  64. <script>
  65. // Crear la función 'Fashion Slider'
  66. function createFashionSlider(el) {
  67. const swiperEl = el.querySelector('.swiper');
  68. let navigationLocked = false;
  69. let transitionDisabled = false;
  70. let frameId;
  71.  
  72. const disableTransitions = (el) => {
  73. el.classList.add('fashion-slider-no-transition');
  74. transitionDisabled = true;
  75. cancelAnimationFrame(frameId);
  76. frameId = requestAnimationFrame(() => {
  77. el.classList.remove('fashion-slider-no-transition');
  78. transitionDisabled = false;
  79. navigationLocked = false;
  80. });
  81. };
  82.  
  83. let fashionSlider;
  84.  
  85. const onNextClick = () => {
  86. if (!navigationLocked) {
  87. fashionSlider.slideNext();
  88. }
  89. };
  90. const onPrevClick = () => {
  91. if (!navigationLocked) {
  92. fashionSlider.slidePrev();
  93. }
  94. };
  95.  
  96. const initNavigation = (swiper) => {
  97. swiper.el
  98. .querySelector('.fashion-slider-button-next')
  99. .addEventListener('click', onNextClick);
  100. swiper.el
  101. .querySelector('.fashion-slider-button-prev')
  102. .addEventListener('click', onPrevClick);
  103. };
  104.  
  105. const destroyNavigation = (swiper) => {
  106. swiper.el
  107. .querySelector('.fashion-slider-button-next')
  108. .removeEventListener('click', onNextClick);
  109. swiper.el
  110. .querySelector('.fashion-slider-button-prev')
  111. .removeEventListener('click', onPrevClick);
  112. };
  113.  
  114. let loopFixed;
  115.  
  116. fashionSlider = new Swiper(swiperEl, {
  117.  
  118. speed: 1300,
  119. allowTouchMove: false,
  120. parallax: true,
  121. on: {
  122. loopFix() {
  123. loopFixed = false;
  124. },
  125. transitionStart(swiper) {
  126. const isLoop = swiper.params.loop;
  127. if (isLoop) {
  128. if (loopFixed) {
  129. return;
  130. }
  131. if (!loopFixed) {
  132. loopFixed = true;
  133. }
  134. }
  135.  
  136. const { slides, previousIndex, activeIndex, el } = swiper;
  137. if (!transitionDisabled) navigationLocked = true;
  138.  
  139. const activeSlide = slides[activeIndex];
  140. const previousSlide = slides[previousIndex];
  141. const previousImageScale = previousSlide.querySelector(
  142. '.fashion-slider-scale',
  143. );
  144. const previousImage = previousSlide.querySelector('img');
  145. const activeImage = activeSlide.querySelector('img');
  146. const direction = activeIndex - previousIndex;
  147. const bgColor = activeSlide.getAttribute('data-slide-bg-color');
  148. el.style['background-color'] = bgColor;
  149.  
  150. previousImageScale.style.transform = 'scale(0.6)';
  151. previousImage.style.transitionDuration = '1000ms';
  152. previousImage.style.transform = 'scale(1.2)';
  153. const previousSlideTitle = previousSlide.querySelector(
  154. '.fashion-slider-title-text',
  155. );
  156. previousSlideTitle.style.transition = '1000ms';
  157. previousSlideTitle.style.color = 'rgba(255,255,255,0)';
  158.  
  159. const onTransitionEnd = (e) => {
  160. if (e.target !== previousImage) return;
  161. previousImage.removeEventListener('transitionend', onTransitionEnd);
  162. activeImage.style.transitionDuration = '1300ms';
  163. activeImage.style.transform = 'translate3d(0, 0, 0) scale(1.2)';
  164. previousImage.style.transitionDuration = '1300ms';
  165. previousImage.style.transform = `translate3d(${
  166. 60 * direction
  167. }%, 0, 0) scale(1.2)`;
  168. };
  169. previousImage.addEventListener('transitionend', onTransitionEnd);
  170. },
  171. transitionEnd(swiper) {
  172. const { slides, activeIndex, el } = swiper;
  173. const activeSlide = slides[activeIndex];
  174. const activeImage = activeSlide.querySelector('img');
  175. const isLoop = swiper.params.loop;
  176.  
  177. activeSlide.querySelector('.fashion-slider-scale').style.transform =
  178. 'scale(1)';
  179. activeImage.style.transitionDuration = '1000ms';
  180. activeImage.style.transform = 'scale(1)';
  181.  
  182. const activeSlideTitle = activeSlide.querySelector(
  183. '.fashion-slider-title-text',
  184. );
  185. activeSlideTitle.style.transition = '1000ms';
  186. activeSlideTitle.style.color = 'rgba(255,255,255,1)';
  187.  
  188. const onTransitionEnd = (e) => {
  189. if (e.target !== activeImage) return;
  190. activeImage.removeEventListener('transitionend', onTransitionEnd);
  191. navigationLocked = false;
  192. };
  193. activeImage.addEventListener('transitionend', onTransitionEnd);
  194. if (!isLoop) {
  195. if (activeIndex === 0) {
  196. el.querySelector('.fashion-slider-button-prev').classList.add(
  197. 'fashion-slider-button-disabled',
  198. );
  199. } else {
  200. el.querySelector('.fashion-slider-button-prev').classList.remove(
  201. 'fashion-slider-button-disabled',
  202. );
  203. }
  204.  
  205. if (activeIndex === slides.length - 1) {
  206. el.querySelector('.fashion-slider-button-next').classList.add(
  207. 'fashion-slider-button-disabled',
  208. );
  209. } else {
  210. el.querySelector('.fashion-slider-button-next').classList.remove(
  211. 'fashion-slider-button-disabled',
  212. );
  213. }
  214. }
  215. },
  216. beforeInit(swiper) {
  217. const { el } = swiper;
  218. disableTransitions(el);
  219. },
  220. init(swiper) {
  221. const { slides, activeIndex, el } = swiper;
  222. const bgColor = slides[activeIndex].getAttribute('data-slide-bg-color');
  223. el.style['background-color'] = bgColor;
  224.  
  225. swiper.emit('transitionEnd');
  226. initNavigation(swiper);
  227. },
  228. resize(swiper) {
  229. disableTransitions(swiper.el);
  230. },
  231. destroy(swiper) {
  232. destroyNavigation(swiper);
  233. },
  234. },
  235. });
  236.  
  237. return fashionSlider;
  238. }
  239. </script>
  240.  
  241. <script>
  242. // Inicializar 'fashion-slider'
  243. const sliderEl = document.querySelector('.fashion-slider');
  244. createFashionSlider(sliderEl);
  245. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement