x4js gallery

Carousel

Slides side by side that move, images or components: arrows, dots, a swipe with the finger or the mouse, the arrow keys and an autoplay. One slide seen at a time, or several.

import { Carousel, CarouselImage } from "x4js"

A slide is an image: src, alt, a caption over its bottom, fit (cover by default). It is loaded when it comes near the view. autoplay goes by itself, paused under the pointer; change gives the index.

export function images( ) {
	const result = new Label( { text: "change: 0" } );

	return new VBox( { content: [
		new Carousel( { height: 260, autoplay: 4000, items: photos, change: ( ev ) => result.setText( `change: ${ev.value}` ) } ),
		result,
	] } );
}

perView shows slides side by side; a move goes one slide further. A slide is any component: a swipe on it is not a click.

export function perView( ) {
	const result = new Label( { text: "Click a card" } );
	const cards = tones.map( ( c, i ) => new VBox( { cls: "card", style: { borderColor: c }, content: [
		new Label( { text: `Card ${i+1}` } ),
		new Button( { label: "Open", cls: "outline", click: ( ) => result.setText( `card ${i+1}` ) } ),
	] } ) );

	return new VBox( { content: [
		new Carousel( { height: 140, perView: 3, items: cards } ),
		result,
	] } );
}
.x4carousel .card {
	height: 100%;
	box-sizing: border-box;
	padding: 12px;
	justify-content: space-between;
	align-items: flex-start;
	border: 2px solid;
	border-radius: 8px;
	--label-color: var( --text-primary );
}

loop: after the last slide, the first. arrows and dots can go; the swipe and the keys (← → Home End, with the focus) stay.

export function commands( ) {
	return new HBox( { content: [
		new Carousel( { width: 280, height: 160, loop: true, arrows: false, items: photos.slice( 0, 4 ) } ),
		new Carousel( { width: 280, height: 160, dots: false, items: photos.slice( 2 ) } ),
	] } );
}

select, next and prev move it without a change event; disabled, it is faded and grey.

export function fromCode( ) {
	const carousel = new Carousel( { height: 200, items: photos } );
	const disabled = new Carousel( { width: 220, height: 120, items: photos, disabled: true } );

	return new VBox( { content: [
		carousel,
		new HBox( { content: [
			new Button( { label: "prev", cls: "outline", click: ( ) => carousel.prev( ) } ),
			new Button( { label: "next", cls: "outline", click: ( ) => carousel.next( ) } ),
			new Button( { label: "select( 4 )", cls: "outline", click: ( ) => carousel.select( 4 ) } ),
		] } ),
		disabled,
	] } );
}

For an assistant: llms.txt, llms-full.txt.