# ImageSlider — SvelteKit UI

Package: sveltekit-ui 1.1.94

Page: https://www.sveltekit-ui.com/components/image_slider

Category: Media

creates a comparison slider between two images, allowing the user to slide and compare before/after views

Public component import: `ImageSlider`

Public manager import: `create_image_slider_manager`

## Usage

```svelte
<script>
  // Import the shared stylesheet once in your application's root layout.
  import "sveltekit-ui/style.css"
  import { ImageSlider, create_image_slider_manager } from "sveltekit-ui"

  // Inline artwork keeps this example independent of image hosting.
  const artwork = (sky) => "data:image/svg+xml," + encodeURIComponent(
    '<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360" viewBox="0 0 640 360">' +
    '<rect width="640" height="360" fill="' + sky + '"/>' +
    '<circle cx="500" cy="90" r="42" fill="#fde68a"/>' +
    '<path d="M0 360 190 115 390 360ZM240 360 430 160 640 360Z" fill="#164e63"/></svg>'
  )

  const manager = create_image_slider_manager({
    src1: artwork("#94a3b8"),
    src2: artwork("#bae6fd"),
    percent_shown: 50,
    aria_label: "Compare original and edited sky",
  })
</script>

<div style="max-width: 64rem;">
  <ImageSlider {manager} />
</div>
<p>Drag the divider or focus it and use the arrow keys.</p>
```

## Configuration

### `src1`

- type: string
- default: null
- description: url for the first/base image

### `src2`

- type: string
- default: null
- description: url for the second or 'comparison' image

### `color1`

- type: string
- default: var(--primary-t)
- description: main color for the center dividing line and handle

### `color2`

- type: string
- default: color-mix(in oklab, var(--primary-t), var(--bg) 80%)
- description: secondary color used to outline the dividing line or handle

### `percent_shown`

- type: number
- default: 50
- description: initial percentage of how much of the second image is shown vs. the first

### `aria_label`

- type: string or function
- default: Image comparison
- description: Accessible name for the keyboard-operable image comparison slider.

## Methods

### `set_percent_shown()`

- description: Updates the image comparison position, clamping finite numeric values to 0–100.
- parameters:
  - input: number or numeric string
- returns: void

### `handle_mount()`

- description: executes on svelte onMount hook, attempts to set aspect ratio based on src1
- parameters:
- returns: void

### `handle_pointer_event()`

- description: manages pointer events (down/move/up) to update the slider percentage when dragging
- parameters:
  - event: pointer or mouse event
  - type: string (down/move/up)
  - id_input: string (the manager’s id to ensure the event belongs to this instance)
- returns: void

## Returned state

### `id`

- type: string
- description: unique identifier for the slider instance

### `src1`

- type: string
- description: url for the first/base image

### `src2`

- type: string
- description: url for the second/compare image

### `color1`

- type: string
- description: primary color used for the divider line and handle

### `color2`

- type: string
- description: secondary outline color for the divider line/handle

### `percent_shown`

- type: number
- description: current slider value (0–100) controlling how much of src2 is shown

### `touch_start_x`

- type: number
- description: tracks the pointer’s x position when the user starts dragging

### `aspect_ratio`

- type: number
- description: the aspect ratio determined by the first image's naturalWidth / naturalHeight

### `button_manager`

- type: object
- description: the button manager for the slider handle’s icon
