Lightweight Vanilla JS Carousel with Touch Support – Driftbox
It registers as a custom <drift-box> HTML element. The element scopes its internal styles and markup with Shadow DOM.
1. Download and load Driftbox as an ES module.
<script type="module" src="./src/driftbox.js"></script>
2. Place your images directly inside the <drift-box> as children. Driftbox reads them via a <slot> and builds the slider track from them. All available configuration options:
autoplay (boolean): Starts slide cycling automatically on page load.interval (number, ms): Sets the milliseconds between slide advances during autoplay. The default is 3000.pause-on-hover (boolean): Stops autoplay when the cursor is over the slider. This only activates on devices that report hover capability.pagination (boolean): Renders a row of dot indicators below the slider track.rounded (boolean): Applies a 10px border-radius to the slider track corners.left (boolean): Left-aligns the slider on the page.center (boolean): Centers the slider on the page. This is the default alignment.right (boolean): Right-aligns the slider on the page.<!-- Configure the carousel with attributes directly on the element --> <drift-box autoplay interval="5000" pause-on-hover pagination rounded> <img src="1.jpg" alt="Alt 1" /> <img src="2.jpg" alt="Alt 2" /> <img src="3.jpg" alt="Alt 3" /> </drift-box>
3. The carousel fills 100% of its host container height by default. Set explicit width and height values in your stylesheet.
drift-box {
width: 75%;
height: 400px;
} 4. Control the slider programmatically by calling methods directly on the DOM node.
// Select the custom element from the DOM
const myCarousel = document.querySelector('drift-box');
// Advance to the next slide manually
myCarousel.next();
// Return to the previous slide manually
myCarousel.prev(); The post Lightweight Vanilla JS Carousel with Touch Support – Driftbox appeared first on CSS Script.
Last week, Capcom added Alex to Street Fighter 6’s playable fighter roster. However, Alex’s arrival…
Last week, Capcom added Alex to Street Fighter 6’s playable fighter roster. However, Alex’s arrival…
Earlier this week, Sony shuttered another of its first-party studios, Dark Outlaw. While we've seen…
Earlier this week, Sony shuttered another of its first-party studios, Dark Outlaw. While we've seen…
If you’re loving Teenage Mutant Ninja Turtles or looking for some great alternative art cards…
If you’re loving Teenage Mutant Ninja Turtles or looking for some great alternative art cards…
This website uses cookies.