Zach’s ugly mug (his face) Zach Leat­herman

<carou-scroll> Web Component

People standing near lighted carousel during night time
956 00 February 02, 2024
🚾Web Component@zachleat/carouscroll

Add next/previous buttons to a horizontal scrollable container.

All🚾 0.2%
Versions
Monthly publishes sparkline for @zachleat/carouscroll, ranging 3 to 4 7 versions in
1.0.6
Audit
0 reports
0 deps
Issues
3
1
PRs
1
3

<carou-scroll> is a zero-dependency web component to add next and previous buttons to a scrollable container. I use it to share exported Keynote slides (as images) on my blog posts.

Some inspiration from the W3C WAI Carousel Tutorial (though this isn’t technically a carousel).

More real-world demos of Keynote slides:

Features

  • Interaction compatible with scroll or touch.
  • No layout shift. Make sure you include the CSS snippet!
  • (Optional) Smooth scrolling with scroll-behavior: smooth.
  • (Optional) loop attribute to enable looping around from start/end.
  • (Optional) Next/Previous buttons can be placed anywhere in the document.
  • (Optional) <output> element can accessibly announce the current slide number (out of total number of slides).

Usage

Installation

You can install via npm or download the carouscroll.js JavaScript file manually.

npm install @zachleat/carouscroll --save

Add carouscroll.js to your site’s JavaScript assets.

Markup

The CSS here is crucial to reduce Layout Shift (CLS), set the aspect ratio of the slides, and to avoid loading loading="lazy" images on off-screen slides.

<style>
carou-scroll {
	display: flex;
	overflow-x: scroll;
	overflow-y: hidden;
}
carou-scroll > * {
	min-width: 100%;
	 /* Customize this as needed */
	aspect-ratio: 16/9;
}
</style>

<script type="module" src="carouscroll.js"></script>

<carou-scroll id="my-scroller">
	<img loading="lazy" src="" alt="">
	<img loading="lazy" src="" alt="">
	<!-- … -->
</carou-scroll>

More examples

Add buttons (optional)

For maximum flexibility, these buttons can be placed anywhere in the document and are tied by an id back to the parent scroller.

Make sure you think about the before/after JavaScript experience here. This component will remove disabled for you but you can add additional styling via your own CSS: carou-scroll:defined {}.

<button type="button" disabled data-carousel-previous="my-scroller">Previous</button>
<button type="button" disabled data-carousel-next="my-scroller">Next</button>

Add output (optional)

This will update (and accessibly announce) a current status element with e.g. Slide 1 of 10 text.

For maximum flexibility, this element can be placed anywhere in the document and is tied by an id back to the parent scroller.

<output data-carousel-output="my-scroller"></output>

<!-- Or customize with your own markup -->
<output data-carousel-output="my-scroller" lang="ko">슬라이드 <span data-carousel-output-current>1</span>/<span data-carousel-output-total>10</span></output>

Make it loop around (optional)

Add the loop attribute.

<carou-scroll id="my-scroller" loop>

Smooth scrolling CSS (optional)

carou-scroll {
	scroll-behavior: smooth;
}

IndieWeb Avatar for https://unsplash.com/Poster image by Sally K

< Older
The Good, The Bad, The Web Components
Newer >
Lessons learned moving Eleventy from CommonJS to ESM

Zach Leatherman IndieWeb Avatar for https://zachleat.com/is a builder for the web at Font Awesome and the creator of Build Awesome (née IndieWeb Avatar for https://www.11ty.devEleventy/11ty), an award-winning open source website generator. He measures website performance with speedlify and at one point became too fixated on web fonts. He has given 89 talks in nine different countries at events like Beyond Tellerrand, Smashing Conference, Jamstack Conf, CSSConf, and The White House. Formerly part of CloudCannon, Netlify, Filament Group, NEJS CONF, and NebraskaJS. Learn more about Zach »

Shamelessly plug your related post

These are webmentions via the IndieWeb and webmention.io.

Sharing on social media?

This is what will show up when you share this post on Social Media:

How did you do this? I automated my Open Graph images. (Peer behind the curtain at the test page)