delagarde

javascript laste working tattoo video site

May 17th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.42 KB | None | 0 0
  1. <script>
  2. document.addEventListener('DOMContentLoaded', function () {
  3.  
  4. // --- Global flag to track if a resize is actively happening ---
  5. let isWindowResizing = false;
  6. let resizeTimeout;
  7.  
  8. window.addEventListener('resize', () => {
  9. isWindowResizing = true;
  10. document.querySelectorAll('.nlb-video-swiper video').forEach(video => {
  11. if (video && !video.paused) video.pause();
  12. });
  13. clearTimeout(resizeTimeout);
  14. resizeTimeout = setTimeout(() => {
  15. isWindowResizing = false;
  16. }, 250);
  17. });
  18. // --- End of global resize handling ---
  19.  
  20. // Helper function to manage the 'controls' attribute
  21. function manageVideoControls(swiper) {
  22. if (!swiper || !swiper.slides || swiper.slides.length === 0) return;
  23.  
  24. swiper.slides.forEach((slideEl, index) => {
  25. const video = slideEl.querySelector('video');
  26. if (video) {
  27. if (index === swiper.activeIndex) {
  28. // Add controls to the active video
  29. if (!video.hasAttribute('controls')) {
  30. video.setAttribute('controls', '');
  31. // console.log(`Controls ADDED to video in active slide ${index}`);
  32. }
  33. } else {
  34. // Remove controls from non-active videos
  35. if (video.hasAttribute('controls')) {
  36. video.removeAttribute('controls');
  37. // console.log(`Controls REMOVED from video in non-active slide ${index}`);
  38. }
  39. }
  40. }
  41. });
  42. }
  43.  
  44. // Function to handle video playback logic for a given Swiper instance
  45. // (Based on Step 12's working version)
  46. function handleVideoPlayback(swiper) {
  47. if (!swiper || swiper.destroyed) { return; }
  48.  
  49. if (isWindowResizing) {
  50. // console.log(`HVPB: Window is resizing, playback deferred. Active: ${swiper.activeIndex}`);
  51. return;
  52. }
  53.  
  54. if (swiper.isFirstHandleVideoCall) {
  55. // console.log(`HVPB First Call (Swiper: ${swiper.el.id}). Active: ${swiper.activeIndex}. Pausing others.`);
  56. swiper.slides.forEach((slideEl, index) => {
  57. const video = slideEl.querySelector('video');
  58. if (video) {
  59. if (index !== swiper.activeIndex) {
  60. if (!video.paused) video.pause();
  61. } else { // Initial active slide
  62. if (!video.paused) video.pause(); // Ensure paused (poster setup done in afterInit)
  63. }
  64. }
  65. });
  66. swiper.isFirstHandleVideoCall = false;
  67. return;
  68. }
  69.  
  70. // console.log(`HVPB Subsequent Call (Swiper: ${swiper.el.id}). Active: ${swiper.activeIndex}. Processing.`);
  71. swiper.slides.forEach((slideEl, index) => {
  72. const video = slideEl.querySelector('video');
  73. if (video) {
  74. if (slideEl.classList.contains('swiper-slide-active')) {
  75. if (video.paused) {
  76. // console.log(`HVPB: Playing video in active slide ${index}`);
  77. if (video.readyState >= 3) {
  78. video.play().catch(error => { console.warn(`Video play (readyState >= 3) in active slide ${index} prevented:`, error); });
  79. } else {
  80. const playWhenReady = () => {
  81. video.play().catch(error => { console.warn(`Video play (on canplay) in active slide ${index} prevented:`, error); });
  82. video.removeEventListener('canplay', playWhenReady);
  83. };
  84. video.removeEventListener('canplay', playWhenReady);
  85. video.addEventListener('canplay', playWhenReady, { once: true });
  86. }
  87. }
  88. } else { // Non-active slides
  89. if (!video.paused) {
  90. video.pause();
  91. // console.log(`HVPB: Paused video in non-active slide ${index}`);
  92. }
  93. }
  94. }
  95. });
  96. }
  97.  
  98. const allSliderWrappers = document.querySelectorAll('.nlb-video-slider-wrapper');
  99.  
  100. allSliderWrappers.forEach((sliderWrapper, sliderIndex) => {
  101. const swiperContainerElement = sliderWrapper.querySelector('.nlb-video-swiper');
  102. if (!swiperContainerElement) return;
  103.  
  104. const paginationElement = sliderWrapper.querySelector('.nlb-video-pagination');
  105. const nextButton = swiperContainerElement.querySelector('.swiper-button-next');
  106. const prevButton = swiperContainerElement.querySelector('.swiper-button-prev');
  107. const swiperSlidesElements = swiperContainerElement.querySelectorAll('.swiper-wrapper .swiper-slide');
  108. const numberOfActualSlides = swiperSlidesElements.length;
  109. let calculatedInitialSlide = 0;
  110. if (numberOfActualSlides > 0) calculatedInitialSlide = Math.ceil(numberOfActualSlides / 2) - 1;
  111.  
  112. swiperContainerElement.querySelectorAll('video').forEach(video => {
  113. if (video && !video.paused) video.pause();
  114. // video.removeAttribute('controls'); // Remove controls from all videos initially
  115. });
  116.  
  117. const nlbVideoSwiper = new Swiper(swiperContainerElement, {
  118. effect: 'cards', grabCursor: true, loop: false, rewind: true,
  119. slidesPerView: 'auto', centeredSlides: true, initialSlide: calculatedInitialSlide,
  120. watchSlidesProgress: true,
  121. cardsEffect: { slideShadows: false, perSlideRotate: 4, perSlideOffset: 32 },
  122. navigation: { nextEl: nextButton, prevEl: prevButton },
  123. pagination: {
  124. el: paginationElement, clickable: true,
  125. renderBullet: function (index, className) {
  126. let posterUrl = ''; const slideElement = this.slides[index];
  127. if (slideElement) {
  128. const videoElement = slideElement.querySelector('video');
  129. if (videoElement && videoElement.hasAttribute('poster')) posterUrl = videoElement.getAttribute('poster');
  130. }
  131. return `<div class="${className}"><img src="${posterUrl}" alt="Thumbnail ${index + 1}"></div>`;
  132. },
  133. },
  134. on: {
  135. init: function (swiper) {
  136. // Initial pause of videos within this instance
  137. swiper.el.querySelectorAll('video').forEach(video => {
  138. if (video && !video.paused) video.pause();
  139. });
  140. // Manage controls attribute state at init (before afterInit fully runs for poster)
  141. // manageVideoControls(swiper); // Potentially call here or ensure afterInit handles it fully
  142. },
  143. afterInit: function(swiper) {
  144. swiper.isFirstHandleVideoCall = true;
  145. manageVideoControls(swiper); // Set controls attribute for active, remove from others
  146.  
  147. if (swiper.slides && swiper.slides.length > swiper.activeIndex) {
  148. const activeSlide = swiper.slides[swiper.activeIndex];
  149. if (activeSlide) {
  150. const video = activeSlide.querySelector('video');
  151. if (video) {
  152. if (!video.paused) video.pause();
  153. video.currentTime = 0;
  154. video.load(); // For initial poster fix
  155. }
  156. }
  157. }
  158. handleVideoPlayback(swiper); // Will run "first call" logic
  159. },
  160. afterResize: function(swiper) {
  161. // console.log(`Swiper instance ${sliderIndex + 1} afterResize. Active: ${swiper.activeIndex}.`);
  162. manageVideoControls(swiper); // Ensure controls are correct after resize
  163. if (swiper.slides && swiper.slides.length > swiper.activeIndex) {
  164. const activeSlide = swiper.slides[swiper.activeIndex];
  165. if (activeSlide) {
  166. const video = activeSlide.querySelector('video');
  167. if (video) {
  168. if (!video.paused) video.pause();
  169. video.currentTime = 0;
  170. }
  171. }
  172. }
  173. // If a transitionEnd happens due to resize, HVPB will check global isWindowResizing
  174. },
  175. transitionStart: function(swiper) {
  176. // Optional: Proactively remove controls from the slide that is *about* to become inactive.
  177. // This might help prevent flicker if transitionEnd is too late.
  178. // However, it can be complex if the transition is reversed or interrupted.
  179. // For now, we rely on transitionEnd.
  180. },
  181. transitionEnd: function (swiper) {
  182. manageVideoControls(swiper); // Update controls attribute for new active/inactive slides
  183. handleVideoPlayback(swiper);
  184. },
  185. click: function (swiper, event) {
  186. const clickedElement = event.target;
  187. if (clickedElement.classList.contains('swiper-video-drag-overlay')) {
  188. let parentSlide = clickedElement.closest('.swiper-slide');
  189. if (parentSlide && parentSlide === swiper.slides[swiper.activeIndex]) {
  190. const video = parentSlide.querySelector('video');
  191. if (video) {
  192. if (video.paused) {
  193. video.play().catch(err => console.warn(`Swiper click play failed`, err));
  194. } else {
  195. video.pause();
  196. }
  197. swiper.isFirstHandleVideoCall = false;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. });
  204. });
  205. });
  206. </script>
Add Comment
Please, Sign In to add comment