Last active
March 18, 2021 18:26
-
-
Save andymagill/8d5e4621f9220137686f3b9520e23384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @file | |
* carousel.js - responsive carousel | |
* @author | |
* Andrew Magill <[email protected]> | |
* | |
*/ | |
(function($) { | |
$(document).ready(function() { | |
initSlides(); | |
}); | |
/** | |
* initializes carousel | |
*/ | |
function initSlides() { | |
var $sliders = $(".leaders > div:not(.slick) > div"); | |
var resizeTimer; | |
toggleSlick($sliders); | |
window.onresize = function(e) { | |
resizeTimer = setTimeout(function() { | |
toggleSlick($sliders); | |
}, 100); | |
} | |
/** | |
* Toggle slick slider | |
* @param {Array} $sliders The slides of the carousel | |
*/ | |
function toggleSlick($sliders) { | |
if ( window.matchMedia("(max-width: 768px)").matches && !$sliders.hasClass('slick-initialized') ) { | |
/* the viewport is less than 768 pixels wide */ | |
$sliders.slick({ | |
infinite: true, | |
slidesToShow: 1, | |
slidesToScroll: 1, | |
dots: true, | |
arrows: true, | |
prevArrow:'<button class="slick-prev slick-arrow" aria-label="Previous" type="button" ><</button>', | |
nextArrow:'<button class="slick-next slick-arrow" aria-label="Next" type="button" >></button>', | |
customPaging: function(slider, i) { | |
return '<svg width="9" height="10" viewBox="0 0 9 10"><g fill="#F6B436"><path d="M6.715 1.5h-4.43l-.043.002c-.187.014-.356.122-.45.288L-.424 5.71l-.021.042c-.08.171-.073.373.021.54l2.216 3.918c.101.18.29.29.493.29h4.43l.044-.002c.186-.014.355-.121.45-.288L9.423 6.29l.021-.042c.08-.172.073-.373-.021-.54L7.208 1.79l-.023-.037c-.105-.157-.281-.253-.47-.253z" transform="translate(-100 -458) translate(13 43) translate(87 414) rotate(90 4.5 6)"/></g></svg>'; | |
}, | |
}); | |
} | |
else if ( !window.matchMedia("(max-width: 768px)").matches && $sliders.hasClass('slick-initialized')) { | |
// if we are not on mobile, get rid of the slider | |
if ( $sliders.hasClass('slick-initialized') ) { | |
$sliders.slick('unslick'); | |
} | |
} | |
} | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment