diff --git a/js/mediaSlider-v2.js b/js/mediaSlider-v2.js new file mode 100644 index 0000000..2060b7d --- /dev/null +++ b/js/mediaSlider-v2.js @@ -0,0 +1,124 @@ +class MediaSlider { + constructor(containerId, mediaUrls) { + this.containerId = containerId; + this.mediaUrls = Array.isArray(mediaUrls) ? mediaUrls : []; + this.observer = null; + this.init(); + } + + init() { + this.injectStyles(); + this.createSlider(); + this.initializeSwiper(); + this.setupLazyLoading(); + } + + setupLazyLoading() { + this.observer = new IntersectionObserver((entries) => { + entries.forEach(entry => { + if (entry.isIntersecting) { + const mediaElement = entry.target; + const actualSrc = mediaElement.dataset.src; + + if (mediaElement.tagName.toLowerCase() === 'img') { + mediaElement.src = actualSrc; + } else if (mediaElement.tagName.toLowerCase() === 'video' || + mediaElement.tagName.toLowerCase() === 'audio') { + mediaElement.src = actualSrc; + mediaElement.load(); + } + + this.observer.unobserve(mediaElement); + } + }); + }, { + root: null, + rootMargin: '50px', + threshold: 0.1 + }); + } + + createMediaElement(url) { + const mediaType = this.detectMediaType(url); + let element; + + switch(mediaType) { + case 'image': + element = `
`; + break; + case 'video': + element = ` `; + break; + case 'audio': + element = ` `; + break; + default: + element = ` `; + } + return element; + } + + createSlider() { + const container = document.getElementById(this.containerId); + const sliderHTML = ` +