A CSS-only carousel drawn as a cover flow
The carousel below is a list with scroll-snap-type, ::scroll-button() and ::scroll-marker. No script scrolls it. It's a child of a canvas,
and the canvas draws each slide as its own textured quad, placed by how far the slide is from the centre of the scroller. Swipe it, use the two buttons or the dots (they are the
generated pseudo-elements, drawn by the canvas), or tab through the Save buttons.
The results are in the Live checks table below.
Show the code CSS carousel, one texture per slide, one matrix per slide
- The list is inside the canvas. The deck is a
drawablechild, and each slide inside the scroller isdrawabletoo. - The carousel is CSS. Scroll snap,
::scroll-button(),::scroll-marker, ascroll-state(snapped)query andsibling-index(). - Where a slide goes. Its distance from the centre of the scroller, in slides, gives a position and a rotation.
- The matrix for one card. CSS pixels of the card to CSS pixels of the canvas, with perspective, then to clip space.
- Hit boxes. One place for every card; CSS decides which card's button is live.
- On
paint. Upload the elements that changed, draw the deck, then the slides from the farthest to the nearest. - The fragment shader. Edge smoothing, shading by tilt, a moving highlight on the centred slide, and the fade of the reflection.
- The upload.
texElementSubImage2D()into a texture of exactly the element's picture size.
How it works
The scroller holds eight empty list items with scroll-snap-align; they're the snap stops and they generate the dots. Each item contains a card that is drawable and positioned against the deck, so it doesn't scroll and isn't clipped. The canvas uploads each card as its own texture and, on every paint event, draws the cards as quads whose position and rotation come from scrollLeft. The deck's own picture (background, the two buttons, the dots) is drawn first on a full-size quad. The canvas repaints on scroll, when a card's picture changes, and for one second after a snap.
Live checks
The button under the demo runs these. They use the demo itself, so it has to be on screen: a point outside the window can't be hit tested.
| Check | Result | Detail |
|---|---|---|
| Every slide inside the scroller has its own texture | not run | |
| The deck's picture does not contain the slides | not run | |
| The Save button of the centred card takes a tap, and the button of a side card does not | not run | |
The scroll-state(snapped) query styles the centred card | not run | |
The current ::scroll-marker is the centred slide's | not run |
Findings
- A
drawableelement nested inside another drawable element is left out of the outer element's picture and has a picture of its own. That's what lets the slides stay inside the scroller: the deck's picture holds the background, the two buttons and the dots, and each card is uploaded separately (Chromium 157). - A drawable card that scrolls with the list gets a partial picture: the scroller clips it, and a card that is half out of the scroller's box is uploaded half empty. So each card is positioned against
the deck (
position: absolute, with no positioned ancestor inside the scroller). It's still a descendant of its slide in the document, which keeps thescroll-state()query, the tab order and the accessibility tree. - Scroll snap uses the place where an element is drawn. When the slides themselves were drawable and had hit boxes at their drawn positions, the scroller snapped to those positions, and a scroll to slide 3 came to rest between two slides. The snap stops are therefore plain list items that the canvas doesn't draw.
- Hit boxes that followed the tilted cards (
updateElementGeometry()with a perspective matrix, or an affine one) worked for hit testing, but cards that had been outside the canvas were drawn empty after they came back, until something else repainted them. Cards with a hit box that stays inside the canvas kept whole pictures. The cause isn't isolated. This page gives every card the hit box of the centred position and lets only the snapped card's button take pointer events, withpointer-eventsset from thescroll-state()query. - A touch on a card scrolls the list because the card has
pointer-events: noneand the scroller is underneath it. Focusing a Save button doesn't scroll the list, because the card doesn't move in layout; afocusinlistener scrolls to the button's slide. ::scroll-button()and::scroll-markerare in the deck's picture, and a tap where they are drawn works. They belong to the scroller, so they can't be drawn apart from the deck without drawing the deck twice.- The
scroll-state(snapped: x)query restyles the centred card with no script. Its transition changes the slide's picture, which raisespaintfor that slide only:changedElementsnames the slides that need a new upload. - The canvas doesn't repaint on a timer. It paints when the list scrolls, when a card's picture changes, and for one second after a snap, while a highlight crosses the centred card.
- Scrolling moves no pixels of any slide's picture, so a scroll alone doesn't always raise
paint. The page callsrequestPaint()from the scroller'sscrollevent. - Not verified here: touch fling on a phone. The test browser is driven by synthetic input.