frontblocks-advanced-option.js
7 months ago
frontblocks-carousel.css
7 months ago
frontblocks-carousel.js
7 months ago
glide.min.js
8 months ago
frontblocks-carousel.js
149 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 | // Check if carousel should be disabled on desktop. |
| 7 | const disableOnDesktop = item.getAttribute('data-disable-on-desktop') === 'true'; |
| 8 | const isDesktop = window.innerWidth >= 768; |
| 9 | |
| 10 | // Skip initialization if disabled on desktop and current viewport is desktop. |
| 11 | if (disableOnDesktop && isDesktop) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | // First Parent. |
| 16 | const parent = item.parentNode; |
| 17 | const wrapper = document.createElement('div'); |
| 18 | |
| 19 | parent.replaceChild(wrapper, item); |
| 20 | wrapper.appendChild(item); |
| 21 | |
| 22 | wrapper.classList.add('glide__track'); |
| 23 | wrapper.setAttribute('data-glide-el', 'track'); |
| 24 | |
| 25 | // Second Parent. |
| 26 | const parentwrap = wrapper.parentNode; |
| 27 | const wrapperParent = document.createElement('div'); |
| 28 | |
| 29 | parentwrap.replaceChild(wrapperParent, wrapper); |
| 30 | wrapperParent.appendChild(wrapper); |
| 31 | wrapperParent.classList.add('glide'); |
| 32 | |
| 33 | // Options |
| 34 | const carouselType = item.getAttribute('data-type') ? item.getAttribute('data-type') : 'carousel'; |
| 35 | const carouselbuttons = item.getAttribute('data-buttons') ? item.getAttribute('data-buttons') : 'bullets'; |
| 36 | const carouselView = item.getAttribute('data-view') ? parseInt(item.getAttribute('data-view')) : 4; |
| 37 | const carouselLaptopView = item.getAttribute('data-laptop-view') ? parseInt(item.getAttribute('data-laptop-view')) : 3; |
| 38 | const carouselTabletView = item.getAttribute('data-tablet-view') ? parseInt(item.getAttribute('data-tablet-view')) : 2; |
| 39 | const carouselMobileView = item.getAttribute('data-mobile-view') ? parseInt(item.getAttribute('data-mobile-view')) : 1; |
| 40 | const carouselAutoplay = item.getAttribute('data-autoplay') ? item.getAttribute('data-autoplay') : 0; |
| 41 | const carouselRewind = item.getAttribute('data-rewind') ? item.getAttribute('data-rewind') : false; |
| 42 | const carouselbuttonsColor = item.getAttribute('data-buttons-color') ? item.getAttribute('data-buttons-color') : 'black'; |
| 43 | const carouselbuttonsBackgroundColor = item.getAttribute('data-buttons-background-color') ? item.getAttribute('data-buttons-background-color') : 'transparent'; |
| 44 | const carouselbuttonsPosition = item.getAttribute('data-buttons-position') ? item.getAttribute('data-buttons-position') : 'side'; |
| 45 | |
| 46 | |
| 47 | // Add classes |
| 48 | item.classList.add('glide__slides', 'glide-' + Math.floor(Math.random() * 1000)); |
| 49 | for (const child of item.children) { |
| 50 | child.classList.add('glide__slide'); |
| 51 | } |
| 52 | |
| 53 | // Don't show bullets on responsive and more than 10 items. |
| 54 | let showBullets = false; |
| 55 | if (window.screen.availWidth < 768 && item.children.length <= 10) { |
| 56 | showBullets = true; |
| 57 | } else if (window.screen.availWidth > 768) { |
| 58 | showBullets = true; |
| 59 | } |
| 60 | |
| 61 | if (showBullets && carouselbuttons == 'bullets') { |
| 62 | const bullets = document.createElement('div'); |
| 63 | bullets.classList.add('glide__bullets'); |
| 64 | bullets.setAttribute('data-glide-el', 'controls[nav]'); |
| 65 | |
| 66 | for (let i = 0; i < item.children.length; i++) { |
| 67 | const bullet = document.createElement('button'); |
| 68 | bullet.classList.add('glide__bullet'); |
| 69 | bullet.setAttribute('data-glide-dir', '=' + i); |
| 70 | bullet.setAttribute('aria-label', 'Go to slide ' + (i + 1)); |
| 71 | bullet.style.backgroundColor = carouselbuttonsBackgroundColor; |
| 72 | bullets.appendChild(bullet); |
| 73 | } |
| 74 | |
| 75 | wrapperParent.appendChild(bullets); |
| 76 | |
| 77 | // Add custom CSS for active bullet color |
| 78 | const style = document.createElement('style'); |
| 79 | style.textContent = ` |
| 80 | .glide__bullet.glide__bullet--active { |
| 81 | background-color: ${carouselbuttonsColor} !important; |
| 82 | } |
| 83 | `; |
| 84 | document.head.appendChild(style); |
| 85 | } |
| 86 | |
| 87 | if (carouselbuttons == 'arrows') { |
| 88 | const arrows = document.createElement('div'); |
| 89 | arrows.classList.add('glide__arrows'); |
| 90 | if (carouselbuttonsPosition == 'bottom') { |
| 91 | arrows.classList.add('glide__arrows--bottom'); |
| 92 | } else { |
| 93 | arrows.classList.add('glide__arrows--top'); |
| 94 | } |
| 95 | |
| 96 | arrows.setAttribute('data-glide-el', 'controls'); |
| 97 | arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"'; |
| 98 | arrowsHTML += ' aria-label="Previous slide"'; |
| 99 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 100 | 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="'; |
| 101 | arrowsHTML += carouselbuttonsColor; |
| 102 | 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=">"'; |
| 103 | arrowsHTML += ' aria-label="Next slide"'; |
| 104 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 105 | 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="'; |
| 106 | arrowsHTML += carouselbuttonsColor; |
| 107 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>'; |
| 108 | arrows.innerHTML = arrowsHTML; |
| 109 | wrapperParent.appendChild(arrows); |
| 110 | } |
| 111 | const glideFrontBlocks = new Glide(wrapperParent, { |
| 112 | type: carouselType, |
| 113 | perView: carouselView, |
| 114 | startAt: 0, |
| 115 | autoplay: carouselAutoplay === 0 ? 2500 : carouselAutoplay, |
| 116 | gap: 20, |
| 117 | rewind: carouselRewind, |
| 118 | breakpoints: { |
| 119 | 768: { |
| 120 | perView: carouselMobileView |
| 121 | }, |
| 122 | 1024: { |
| 123 | perView: carouselTabletView |
| 124 | }, |
| 125 | 1440: { |
| 126 | perView: carouselLaptopView |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | |
| 131 | if ('slider' === carouselType) { |
| 132 | glideFrontBlocks.on('run.after', () => { |
| 133 | const currentIndex = glideFrontBlocks.index; |
| 134 | const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length; |
| 135 | |
| 136 | actualView = parseInt(currentIndex) + parseInt(carouselView); |
| 137 | |
| 138 | if (actualView > lastIndex) { |
| 139 | setTimeout(() => { |
| 140 | glideFrontBlocks.go('=0'); |
| 141 | }, 5); |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | glideFrontBlocks.mount(); |
| 147 | }); |
| 148 | } |
| 149 | }); |