PluginProbe
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor / 2.0.7
Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor v2.0.7
2.0.13 2.0.12 2.0.11 2.0.10 2.0.9 trunk 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8
blockenberg / blocks / image-slider / frontend.js

frontend.js in Blockenberg — 600+ Advanced Gutenberg Blocks & AI Agent for WordPress Block Editor 2.0.7, at blocks/image-slider/frontend.js

103 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
4 function initSlider(wrapper) {
5 var slider = wrapper.querySelector('.bkbg-is-slider');
6 if (!slider) return;
7
8 var slides = Array.prototype.slice.call(wrapper.querySelectorAll('.bkbg-is-slide'));
9 var dots = Array.prototype.slice.call(wrapper.querySelectorAll('.bkbg-is-dot'));
10 var prevBtn = wrapper.querySelector('.bkbg-is-arrow-prev');
11 var nextBtn = wrapper.querySelector('.bkbg-is-arrow-next');
12 var total = slides.length;
13 if (total < 2) return;
14
15 var current = 0;
16 var autoplay = wrapper.dataset.autoplay === '1';
17 var speed = parseInt(wrapper.dataset.speed, 10) || 4000;
18 var loop = wrapper.dataset.loop === '1';
19 var pauseHover = wrapper.dataset.pauseHover === '1';
20 var transition = wrapper.dataset.transition || 'slide';
21 var timer = null;
22 var touching = false;
23 var touchStartX = 0;
24
25 function goTo(n) {
26 if (!loop && (n < 0 || n >= total)) return;
27 var prev = current;
28 current = (n + total) % total;
29 if (prev === current) return;
30
31 slides[prev].classList.remove('bkbg-is-slide-active');
32 if (transition === 'slide') slides[prev].classList.add('bkbg-is-slide-exit');
33
34 slides[current].classList.add('bkbg-is-slide-active');
35 if (dots[prev]) dots[prev].classList.remove('bkbg-is-dot-active');
36 if (dots[current]) dots[current].classList.add('bkbg-is-dot-active');
37
38 if (transition === 'slide') {
39 var exPrev = prev;
40 setTimeout(function () { slides[exPrev].classList.remove('bkbg-is-slide-exit'); }, 600);
41 }
42 }
43
44 function next() { goTo(current + 1); }
45 function prev() { goTo(current - 1); }
46
47 function startTimer() {
48 if (!autoplay) return;
49 timer = setInterval(next, speed);
50 }
51
52 function stopTimer() { clearInterval(timer); timer = null; }
53
54 if (nextBtn) nextBtn.addEventListener('click', function () { stopTimer(); next(); startTimer(); });
55 if (prevBtn) prevBtn.addEventListener('click', function () { stopTimer(); prev(); startTimer(); });
56
57 dots.forEach(function (dot, idx) {
58 dot.addEventListener('click', function () { stopTimer(); goTo(idx); startTimer(); });
59 });
60
61 if (pauseHover) {
62 wrapper.addEventListener('mouseenter', stopTimer);
63 wrapper.addEventListener('mouseleave', function () { if (autoplay) startTimer(); });
64 }
65
66 /* Touch/swipe */
67 wrapper.addEventListener('touchstart', function (e) {
68 touching = true;
69 touchStartX = e.touches[0].clientX;
70 }, { passive: true });
71
72 wrapper.addEventListener('touchend', function (e) {
73 if (!touching) return;
74 touching = false;
75 var delta = touchStartX - e.changedTouches[0].clientX;
76 if (Math.abs(delta) > 40) {
77 stopTimer();
78 delta > 0 ? next() : prev();
79 startTimer();
80 }
81 });
82
83 /* Keyboard support */
84 wrapper.setAttribute('tabindex', '0');
85 wrapper.addEventListener('keydown', function (e) {
86 if (e.key === 'ArrowLeft') { stopTimer(); prev(); startTimer(); }
87 if (e.key === 'ArrowRight') { stopTimer(); next(); startTimer(); }
88 });
89
90 startTimer();
91 }
92
93 function init() {
94 document.querySelectorAll('.bkbg-is-wrapper').forEach(initSlider);
95 }
96
97 if (document.readyState === 'loading') {
98 document.addEventListener('DOMContentLoaded', init);
99 } else {
100 init();
101 }
102 })();
103