// This is the carousel implemented in idiomatic javascript // Converts NodeLists to Arrays function toArray(x) { return Array.prototype.slice.call(x); }; // Returns array of elements that match css query nested to parent function queryChildren(parent, query) { return toArray(Element.prototype.querySelectorAll.call(parent, query)); }; // Returns array of elements that match css query function queryAll(query) { return toArray(document.querySelectorAll(query)); }; // Sets the hidden property of elements based on new index function to(index, children) { children.forEach((function(x, i) { x.hidden = i !== index; })); }; // Gets the current index of carousel function index(children) { return (Array.prototype.findIndex.call(children, (function(x) { return !(x.hidden); })) || 0); }; // Moves to next slide in carousel with wrapping function rotate(children) { return to(((1 + index(children)) % children.length), children); }; // Rotate carousels once every three seconds queryAll("[data-carousel]").forEach((function(x) { return window.setInterval(rotate.bind(window, queryChildren(x, "[data-carousel-slide]")), 3000); }));