< >

"Spread of cards" responsive carousel layout 3.0

The third major fork of the spread of cards layout ( v2.0 here ), which actually is much simpler, but was born out of a bug in Mac/iOS Safari>=v8.1 where consectutive sibling selectors (eg .pointer+li+li+li) gave random results. This version replaces multiple consecutive sibling selectors ("+") with the general sibling selector ("~") which works in Safari. That's the positive change from v2.0, the negative is that it doesn't have the neat single-card-wide stack off to the right, but a train of side-by-side cards. That means it's not so easy to track which cards are off-screen right (but offscreen left is still a stack), so for example you can't trigger "offscreen" changes such as fade opacity.


From original codepen:

If you really need a carousel, you need a responsive one.

This demo is just an early test of a different sort of carousel layout. Generally you need javascript to be in control of the not only the events, but monitoring for viewport size changes, setting the size of the carousel and the position of the cards on and off canvas.

The tricky bit is that the CSS - not the JS - is controlling the media queries and the basic layout. So why fight it with JS resize events and setting explicitly defined sizes? In this demo, JS only changes one thing in the DOM when you click left or right - it selects the next card that is the first visible one.

Everything else is taken care of in CSS.

1. It's not just one long (mostly off-canvas) strip, which still uses up compositing memory, even if it's not painted to the viewable screen. Instead, it uses a stack off cards left and right. Hopefully that means using less memory (depending on your browser).

2. As a byproduct of the stacks offscreen, you get a neat animation.

3. Because there are no floats or absolute positioning, the height of the overall carousel is controlled only by the content, containers don't need a complimentary fixed height to maintain shape.

4. Design decisions like the width of the carousel, the cards and how many cards (across all screen sizes) are much easier to deal with in the stuff that defines them - the CSS. Not to mention the reduction in JS events fired by window resizing - and the jank that causes.

5. This was originally built to work with an angular.js website (not like the jQ version here) and it suits that pattern as each card can have a ng-class directive and the controller just changes a value in the scope to activate the first card you want shown.