frontblocks-carousel.css
1 year ago
frontblocks-carousel.js
1 year ago
frontblocks-carousel.php
1 year ago
frontblocks-carousel.js
104 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 | carouselType = item.getAttribute('data-type') ? item.getAttribute('data-type') : 'carousel'; |
| 26 | carouselbuttons = item.getAttribute('data-buttons') ? item.getAttribute('data-buttons') : 'bullets'; |
| 27 | carouselView = item.getAttribute('data-view') ? item.getAttribute('data-view') : 4; |
| 28 | carouselAutoplay = item.getAttribute('data-autoplay') ? item.getAttribute('data-autoplay') : 0; |
| 29 | carouselResView = item.getAttribute('data-res-view') ? item.getAttribute('data-res-view') : 1; |
| 30 | |
| 31 | carouselbuttonsColor = item.getAttribute('data-buttons-color') ? item.getAttribute('data-buttons-color') : 'black'; |
| 32 | carouselbuttonsBackgroundColor = item.getAttribute('data-buttons-background-color') ? item.getAttribute('data-buttons-background-color') : 'transparent'; |
| 33 | |
| 34 | |
| 35 | // Add classes |
| 36 | item.classList.add( 'glide__slides', 'glide-' + Math.floor(Math.random() * 1000) ); |
| 37 | for (const child of item.children) { |
| 38 | child.classList.add('glide__slide'); |
| 39 | } |
| 40 | |
| 41 | // Don't show bullets on responsive and more than 6 items. |
| 42 | if ( window.screen.availWidth < 768 && item.children.length < 6 ) { |
| 43 | showBullets = true; |
| 44 | } else if ( window.screen.availWidth > 768 ) { |
| 45 | showBullets = true; |
| 46 | } else { |
| 47 | showBullets = false; |
| 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 | |
| 66 | if ( carouselbuttons == 'arrows' ) { |
| 67 | const arrows = document.createElement('div'); |
| 68 | arrows.classList.add('glide__arrows'); |
| 69 | arrows.setAttribute('data-glide-el', 'controls'); |
| 70 | arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"'; |
| 71 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 72 | 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="'; |
| 73 | arrowsHTML += carouselbuttonsColor; |
| 74 | 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=">"'; |
| 75 | |
| 76 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 77 | 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="'; |
| 78 | arrowsHTML += carouselbuttonsColor; |
| 79 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>'; |
| 80 | arrows.innerHTML = arrowsHTML; |
| 81 | wrapperParent.appendChild(arrows); |
| 82 | } |
| 83 | |
| 84 | new Glide( wrapperParent, { |
| 85 | type: carouselType, |
| 86 | perView: carouselView, |
| 87 | startAt: 0, |
| 88 | autoplay: carouselAutoplay === 0 ? 2500 : carouselAutoplay, |
| 89 | gap: 0, |
| 90 | breakpoints: { |
| 91 | 430: { |
| 92 | perView: carouselResView |
| 93 | }, |
| 94 | 600: { |
| 95 | perView: carouselResView |
| 96 | }, |
| 97 | 768: { |
| 98 | perView: carouselResView |
| 99 | }, |
| 100 | } |
| 101 | }).mount(); |
| 102 | }); |
| 103 | } |
| 104 | }); |