1 line
2.5 KiB
JavaScript
1 line
2.5 KiB
JavaScript
(()=>{(function(){function c(n){if(!n||n.hasAttribute("data-initialized"))return;n.setAttribute("data-initialized","true");let o=n.querySelector(".carousel-track"),f=Array.from(o?.querySelectorAll(".carousel-item")||[]);if(f.length===0)return;let p=Array.from(n.querySelectorAll(".carousel-indicator")),i=n.querySelector(".carousel-prev"),a=n.querySelector(".carousel-next"),t={currentIndex:0,slideCount:f.length,autoplay:n.dataset.autoplay==="true",interval:parseInt(n.dataset.interval||5e3),loop:n.dataset.loop==="true",autoplayInterval:null,isHovering:!1,touchStartX:0};function v(){o.style.transform=`translateX(-${t.currentIndex*100}%)`}function y(){p.forEach((e,l)=>{l<t.slideCount?(l===t.currentIndex?(e.classList.add("bg-white"),e.classList.remove("bg-white/50")):(e.classList.remove("bg-white"),e.classList.add("bg-white/50")),e.style.display=""):e.style.display="none"})}function I(){i&&(i.disabled=!t.loop&&t.currentIndex===0,i.classList.toggle("opacity-50",i.disabled),i.classList.toggle("cursor-not-allowed",i.disabled)),a&&(a.disabled=!t.loop&&t.currentIndex===t.slideCount-1,a.classList.toggle("opacity-50",a.disabled),a.classList.toggle("cursor-not-allowed",a.disabled))}function s(){t.autoplayInterval&&clearInterval(t.autoplayInterval),t.autoplay&&(t.autoplayInterval=setInterval(()=>{t.isHovering||r()},t.interval))}function g(){t.autoplayInterval&&(clearInterval(t.autoplayInterval),t.autoplayInterval=null)}function r(){let e=t.currentIndex+1;if(e>=t.slideCount)if(t.loop)e=0;else return;u(e)}function h(){let e=t.currentIndex-1;if(e<0)if(t.loop)e=t.slideCount-1;else return;u(e)}function u(e){if(e<0||e>=t.slideCount)if(t.loop)e=(e+t.slideCount)%t.slideCount;else return;e!==t.currentIndex&&(t.currentIndex=e,v(),y(),I(),t.autoplay&&!t.isHovering&&(g(),s()))}o&&(o.addEventListener("touchstart",e=>{t.touchStartX=e.touches[0].clientX},{passive:!0}),o.addEventListener("touchend",e=>{let l=e.changedTouches[0].clientX,m=t.touchStartX-l;Math.abs(m)>50&&(m>0?r():h())},{passive:!0})),p.forEach((e,l)=>{l<t.slideCount&&e.addEventListener("click",()=>u(l))}),i&&i.addEventListener("click",h),a&&a.addEventListener("click",r),n.addEventListener("mouseenter",()=>{t.isHovering=!0,t.autoplay&&g()}),n.addEventListener("mouseleave",()=>{t.isHovering=!1,t.autoplay&&s()}),v(),y(),I(),t.autoplay&&s()}function d(n=document){n instanceof Element&&n.matches(".carousel-component")&&c(n);for(let o of n.querySelectorAll(".carousel-component:not([data-initialized])"))c(o)}window.templUI=window.templUI||{},window.templUI.carousel={init:d},document.addEventListener("DOMContentLoaded",()=>d())})();})();
|