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 / post-slider / frontend.js

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

92 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* Post Slider — frontend.js */
2 ( function () {
3 function initSlider( wrap ) {
4 const track = wrap.querySelector( '.bkps-track' );
5 const slides = Array.from( track ? track.querySelectorAll( '.bkps-slide' ) : [] );
6 if ( slides.length < 2 ) return;
7
8 const autoplay = wrap.dataset.autoplay === '1';
9 const speed = parseInt( wrap.dataset.speed ) || 4000;
10 const isSlide = wrap.classList.contains( 'bkps-transition-slide' );
11 const showDots = wrap.dataset.dots === '1';
12
13 let current = 0;
14 let timer = null;
15
16 /* Build dots dynamically */
17 if ( showDots ) {
18 const dotsWrap = wrap.querySelector( '.bkps-dots' );
19 if ( dotsWrap ) {
20 slides.forEach( (_, i) => {
21 const dot = document.createElement( 'button' );
22 dot.className = 'bkps-dot' + ( i === 0 ? ' bkps-dot-active' : '' );
23 dot.addEventListener( 'click', () => { clearInterval(timer); goTo(i); startAutoplay(); } );
24 dotsWrap.appendChild( dot );
25 } );
26 }
27 }
28
29 function updateDots() {
30 wrap.querySelectorAll( '.bkps-dot' ).forEach( (d,i) => d.classList.toggle('bkps-dot-active', i === current) );
31 }
32
33 function goTo( idx ) {
34 const prev = current;
35 current = ( idx + slides.length ) % slides.length;
36
37 if ( isSlide ) {
38 slides[prev].classList.add('bkps-prev-slide');
39 slides[prev].classList.remove('bkps-active');
40 slides[current].classList.add('bkps-active');
41 setTimeout( () => slides[prev].classList.remove('bkps-prev-slide'), 650 );
42 } else {
43 slides[prev].classList.remove('bkps-active');
44 slides[current].classList.add('bkps-active');
45 }
46 updateDots();
47 }
48
49 /* Arrows */
50 const prevBtn = wrap.querySelector('.bkps-prev');
51 const nextBtn = wrap.querySelector('.bkps-next');
52 if ( prevBtn ) prevBtn.addEventListener('click', () => { clearInterval(timer); goTo(current-1); startAutoplay(); });
53 if ( nextBtn ) nextBtn.addEventListener('click', () => { clearInterval(timer); goTo(current+1); startAutoplay(); });
54
55 /* Touch swipe */
56 let touchX = 0;
57 wrap.addEventListener('touchstart', e => { touchX = e.changedTouches[0].clientX; }, {passive:true});
58 wrap.addEventListener('touchend', e => {
59 const dx = e.changedTouches[0].clientX - touchX;
60 if ( Math.abs(dx) > 50 ) { clearInterval(timer); goTo( dx < 0 ? current+1 : current-1 ); startAutoplay(); }
61 }, {passive:true});
62
63 /* Keyboard */
64 wrap.setAttribute('tabindex', '0');
65 wrap.addEventListener('keydown', e => {
66 if (e.key==='ArrowLeft') { clearInterval(timer); goTo(current-1); startAutoplay(); }
67 if (e.key==='ArrowRight') { clearInterval(timer); goTo(current+1); startAutoplay(); }
68 });
69
70 function startAutoplay() {
71 if ( !autoplay ) return;
72 clearInterval(timer);
73 timer = setInterval( () => goTo(current+1), speed );
74 }
75
76 wrap.addEventListener('mouseenter', () => clearInterval(timer));
77 wrap.addEventListener('mouseleave', startAutoplay);
78
79 startAutoplay();
80 }
81
82 function init() {
83 document.querySelectorAll('.bkps-wrap[data-autoplay]').forEach( initSlider );
84 }
85
86 if ( document.readyState === 'loading' ) {
87 document.addEventListener('DOMContentLoaded', init);
88 } else {
89 init();
90 }
91 }() );
92