Advertisement
oscarviedma

Código Javascript integracción y funcionalidad swiper.js - CSY

Aug 3rd, 2024
1,276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.07 KB | None | 0 0
  1. <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css">
  2. <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
  3.  
  4. <script>
  5. // Funcionalidad Swiper
  6. const swiper = new Swiper('.swiper-carousel-shorts', {
  7.     slidesPerView: 5,
  8.     spaceBetween: 25,
  9.     slidesPerGroup: 1,
  10.     loop: false,
  11.     loopFillGroupWithBlank: true,
  12.     navigation: {
  13.         nextEl: '.swiper-button-next',
  14.         prevEl: '.swiper-button-prev',
  15.     },
  16.     breakpoints: {
  17.         1080: {
  18.             slidesPerView: 5,
  19.         },
  20.         981: {
  21.             slidesPerView: 4,
  22.         },
  23.         769: {
  24.             slidesPerView: 3,
  25.         },
  26.         0: {
  27.             slidesPerView: 2,
  28.         },
  29.     },
  30. });
  31.  
  32. const popup = document.querySelector('.popup');
  33. const popupVideo = document.querySelector('.popup-video');
  34. const popupClose = document.querySelector('.popup-close');
  35. const popupPrev = document.querySelector('.popup-prev');
  36. const popupNext = document.querySelector('.popup-next');
  37. const slides = document.querySelectorAll('.swiper-slide');
  38. const mainHeader = document.getElementById('main-header');
  39. let currentVideoIndex = 0;
  40.  
  41. slides.forEach((slide, index) => {
  42.     slide.addEventListener('click', () => openPopup(index));
  43. });
  44.  
  45. function openPopup(index) {
  46.     currentVideoIndex = index;
  47.     updatePopupVideo();
  48.     popup.style.display = 'block';
  49.     mainHeader.style.zIndex = '0';
  50.     document.addEventListener('keydown', handleEscKey);
  51. }
  52.  
  53. function closePopup() {
  54.     popup.style.display = 'none';
  55.     popupVideo.src = '';
  56.     mainHeader.style.zIndex = '';
  57.     document.removeEventListener('keydown', handleEscKey);
  58. }
  59.  
  60. function updatePopupVideo() {
  61.     const slide = slides[currentVideoIndex];
  62.     const videoClass = slide.className.split(' ').find(cls => cls.startsWith('video-'));
  63.     if (videoClass) {
  64.         const videoId = videoClass.split('video-')[1];
  65.         popupVideo.src = `https://www.youtube.com/embed/${videoId}?autoplay=1&showinfo=0&rel=0&modestbranding=1`;
  66.     } else {
  67.         console.error('No se encontró la clase de video para esta diapositiva');
  68.     }
  69. }
  70.  
  71. function handleEscKey(event) {
  72.     if (event.key === 'Escape') {
  73.         closePopup();
  74.     } else if (event.key === 'ArrowLeft') {
  75.         // Flecha izquierda para video anterior
  76.         currentVideoIndex = (currentVideoIndex - 1 + slides.length) % slides.length;
  77.         updatePopupVideo();
  78.     } else if (event.key === 'ArrowRight') {
  79.         // Flecha derecha para siguiente video
  80.         currentVideoIndex = (currentVideoIndex + 1) % slides.length;
  81.         updatePopupVideo();
  82.     }
  83. }
  84.  
  85. popup.addEventListener('click', (event) => {
  86.     if (event.target === popup) {
  87.         closePopup();
  88.     }
  89. });
  90.  
  91. popupClose.addEventListener('click', closePopup);
  92. popupPrev.addEventListener('click', () => {
  93.     currentVideoIndex = (currentVideoIndex - 1 + slides.length) % slides.length;
  94.     updatePopupVideo();
  95. });
  96. popupNext.addEventListener('click', () => {
  97.     currentVideoIndex = (currentVideoIndex + 1) % slides.length;
  98.     updatePopupVideo();
  99. });
  100. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement