| 1 |
(function () { |
| 2 |
function initCarousel(root) { |
| 3 |
var track = root.querySelector(".aibui-carousel-track"); |
| 4 |
if (!track) return; |
| 5 |
var slides = root.querySelectorAll(".aibui-carousel-slide"); |
| 6 |
var dots = root.querySelectorAll(".aibui-carousel-dot"); |
| 7 |
var prev = root.querySelector(".aibui-carousel-prev"); |
| 8 |
var next = root.querySelector(".aibui-carousel-next"); |
| 9 |
var idx = 0; |
| 10 |
var autoplay = root.getAttribute("data-autoplay") === "1"; |
| 11 |
var delay = parseInt(root.getAttribute("data-delay") || "4", 10) * 1000; |
| 12 |
function getViewportWidth() { |
| 13 |
var holder = root.querySelector(".aibui-carousel-preview") || root; |
| 14 |
return holder.clientWidth || holder.offsetWidth || 0; |
| 15 |
} |
| 16 |
function update() { |
| 17 |
var w = getViewportWidth(); |
| 18 |
track.style.transform = "translateX(" + -idx * w + "px)"; |
| 19 |
dots.forEach(function (d, i) { |
| 20 |
d.classList.toggle("is-active", i === idx); |
| 21 |
}); |
| 22 |
} |
| 23 |
function go(n) { |
| 24 |
idx = (n + slides.length) % slides.length; |
| 25 |
update(); |
| 26 |
} |
| 27 |
if (prev) |
| 28 |
prev.addEventListener("click", function () { |
| 29 |
go(idx - 1); |
| 30 |
}); |
| 31 |
if (next) |
| 32 |
next.addEventListener("click", function () { |
| 33 |
go(idx + 1); |
| 34 |
}); |
| 35 |
var timer; |
| 36 |
function start() { |
| 37 |
if (autoplay && slides.length > 1) { |
| 38 |
stop(); |
| 39 |
timer = setInterval(function () { |
| 40 |
go(idx + 1); |
| 41 |
}, delay); |
| 42 |
} |
| 43 |
} |
| 44 |
function stop() { |
| 45 |
if (timer) { |
| 46 |
clearInterval(timer); |
| 47 |
timer = null; |
| 48 |
} |
| 49 |
} |
| 50 |
root.addEventListener("mouseenter", stop); |
| 51 |
root.addEventListener("mouseleave", start); |
| 52 |
dots.forEach(function (d, i) { |
| 53 |
d.addEventListener("click", function () { |
| 54 |
go(i); |
| 55 |
}); |
| 56 |
}); |
| 57 |
update(); |
| 58 |
start(); |
| 59 |
// keep alignment on resize |
| 60 |
window.addEventListener("resize", update); |
| 61 |
} |
| 62 |
function boot() { |
| 63 |
var roots = document.querySelectorAll( |
| 64 |
".wp-block-ai-builder-aibui-carousel" |
| 65 |
); |
| 66 |
for (var i = 0; i < roots.length; i++) { |
| 67 |
initCarousel(roots[i]); |
| 68 |
} |
| 69 |
} |
| 70 |
if (document.readyState === "loading") { |
| 71 |
document.addEventListener("DOMContentLoaded", boot); |
| 72 |
} else { |
| 73 |
boot(); |
| 74 |
} |
| 75 |
})(); |
| 76 |
|