frontblocks-advanced-option.jsx
11 months ago
frontblocks-carousel.css
11 months ago
frontblocks-carousel.js
11 months ago
frontblocks-carousel.php
10 months ago
frontblocks-carousel.js
136 lines
| 1 | window.addEventListener('load', function (event) { |
| 2 | const carouselItem = document.querySelectorAll('.frontblocks-carousel'); |
| 3 | |
| 4 | if (carouselItem.length > 0) { |
| 5 | carouselItem.forEach( (item) => { |
| 6 | // First Parent. |
| 7 | const parent = item.parentNode; |
| 8 | const wrapper = document.createElement('div'); |
| 9 | |
| 10 | parent.replaceChild(wrapper, item); |
| 11 | wrapper.appendChild(item); |
| 12 | |
| 13 | wrapper.classList.add('glide__track'); |
| 14 | wrapper.setAttribute('data-glide-el', 'track'); |
| 15 | |
| 16 | // Second Parent. |
| 17 | const parentwrap = wrapper.parentNode; |
| 18 | const wrapperParent = document.createElement('div'); |
| 19 | |
| 20 | parentwrap.replaceChild(wrapperParent, wrapper); |
| 21 | wrapperParent.appendChild(wrapper); |
| 22 | wrapperParent.classList.add('glide'); |
| 23 | |
| 24 | // Options |
| 25 | const carouselType = item.getAttribute('data-type') ? item.getAttribute('data-type') : 'carousel'; |
| 26 | const carouselbuttons = item.getAttribute('data-buttons') ? item.getAttribute('data-buttons') : 'bullets'; |
| 27 | const carouselView = item.getAttribute('data-view') ? item.getAttribute('data-view') : 4; |
| 28 | const carouselAutoplay = item.getAttribute('data-autoplay') ? item.getAttribute('data-autoplay') : 0; |
| 29 | const carouselResView = item.getAttribute('data-res-view') ? item.getAttribute('data-res-view') : 1; |
| 30 | const carouselRewind = item.getAttribute('data-rewind') ? item.getAttribute('data-rewind') : false; |
| 31 | const carouselbuttonsColor = item.getAttribute('data-buttons-color') ? item.getAttribute('data-buttons-color') : 'black'; |
| 32 | const carouselbuttonsBackgroundColor = item.getAttribute('data-buttons-background-color') ? item.getAttribute('data-buttons-background-color') : 'transparent'; |
| 33 | const carouselbuttonsPosition = item.getAttribute('data-buttons-position') ? item.getAttribute('data-buttons-position') : 'side'; |
| 34 | |
| 35 | |
| 36 | // Add classes |
| 37 | item.classList.add( 'glide__slides', 'glide-' + Math.floor(Math.random() * 1000) ); |
| 38 | for (const child of item.children) { |
| 39 | child.classList.add('glide__slide'); |
| 40 | } |
| 41 | |
| 42 | // Don't show bullets on responsive and more than 6 items. |
| 43 | let showBullets = false; |
| 44 | if ( window.screen.availWidth < 768 && item.children.length < 6 ) { |
| 45 | showBullets = true; |
| 46 | } else if ( window.screen.availWidth > 768 ) { |
| 47 | showBullets = true; |
| 48 | } |
| 49 | |
| 50 | if ( showBullets && carouselbuttons == 'bullets' ) { |
| 51 | const bullets = document.createElement('div'); |
| 52 | bullets.classList.add('glide__bullets'); |
| 53 | bullets.setAttribute('data-glide-el', 'controls[nav]'); |
| 54 | |
| 55 | for (let i = 0; i < item.children.length; i++) { |
| 56 | const bullet = document.createElement('button'); |
| 57 | bullet.classList.add('glide__bullet'); |
| 58 | bullet.setAttribute('data-glide-dir', '=' + i); |
| 59 | bullet.style.backgroundColor = carouselbuttonsBackgroundColor; |
| 60 | bullets.appendChild(bullet); |
| 61 | } |
| 62 | |
| 63 | wrapperParent.appendChild(bullets); |
| 64 | |
| 65 | // Add custom CSS for active bullet color |
| 66 | const style = document.createElement('style'); |
| 67 | style.textContent = ` |
| 68 | .glide__bullet.glide__bullet--active { |
| 69 | background-color: ${carouselbuttonsColor} !important; |
| 70 | } |
| 71 | `; |
| 72 | document.head.appendChild(style); |
| 73 | } |
| 74 | |
| 75 | if ( carouselbuttons == 'arrows' ) { |
| 76 | const arrows = document.createElement('div'); |
| 77 | arrows.classList.add('glide__arrows'); |
| 78 | if ( carouselbuttonsPosition == 'bottom' ) { |
| 79 | arrows.classList.add('glide__arrows--bottom'); |
| 80 | } else { |
| 81 | arrows.classList.add('glide__arrows--top'); |
| 82 | } |
| 83 | |
| 84 | arrows.setAttribute('data-glide-el', 'controls'); |
| 85 | arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"'; |
| 86 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 87 | arrowsHTML += '><svg width="7" height="12" viewBox="0 0 7 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 1L1 6L6 11" stroke="'; |
| 88 | arrowsHTML += carouselbuttonsColor; |
| 89 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button><button class="glide__arrow glide__arrow--right glide__arrow glide__arrow--right" data-glide-dir=">"'; |
| 90 | |
| 91 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 92 | arrowsHTML += '><svg width="7" height="12" viewBox="0 0 7 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 11L6 6L1 1" stroke="'; |
| 93 | arrowsHTML += carouselbuttonsColor; |
| 94 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>'; |
| 95 | arrows.innerHTML = arrowsHTML; |
| 96 | wrapperParent.appendChild(arrows); |
| 97 | } |
| 98 | const glideFrontBlocks = new Glide( wrapperParent, { |
| 99 | type: carouselType, |
| 100 | perView: carouselView, |
| 101 | startAt: 0, |
| 102 | autoplay: false, //carouselAutoplay === 0 ? 2500 : carouselAutoplay, |
| 103 | gap: 0, |
| 104 | rewind: carouselRewind, |
| 105 | breakpoints: { |
| 106 | 430: { |
| 107 | perView: carouselResView |
| 108 | }, |
| 109 | 600: { |
| 110 | perView: carouselResView |
| 111 | }, |
| 112 | 768: { |
| 113 | perView: carouselResView |
| 114 | }, |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | if ( 'slider' === carouselType ) { |
| 119 | glideFrontBlocks.on('run.after', () => { |
| 120 | const currentIndex = glideFrontBlocks.index; |
| 121 | const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length; |
| 122 | |
| 123 | actualView = parseInt(currentIndex) + parseInt(carouselView); |
| 124 | |
| 125 | if ( actualView > lastIndex ) { |
| 126 | setTimeout(() => { |
| 127 | glideFrontBlocks.go('=0'); |
| 128 | }, 5); |
| 129 | } |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | glideFrontBlocks.mount(); |
| 134 | }); |
| 135 | } |
| 136 | }); |