Controls · Angular, React and OpenLayers

Zoom sliders in three styles

Style OpenLayers ZoomSlider controls in three map layouts: default vertical rail, tucked between zoom buttons, and a compact horizontal bar.

Three street maps of central Kraków under their headings, each with a zoom slider: below the zoom buttons, between them, and as a small orange bar in the top right corner.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Three stacked street maps of central Kraków showcase how CSS alone transforms the exact same OpenLayers ZoomSlider control: first in its default vertical track beneath the +/− buttons, second nestled seamlessly between the + and − buttons as a unified navigation bar, and third restyled as a compact horizontal track in the top-right corner.

Dragging any thumb scales the map continuously, while clicking anywhere along the rail animates smoothly to that zoom level in 200 milliseconds.

When you need it

Step-by-step + and − buttons require tedious repeated clicks to jump from a national overview down to parcel scale and give no visual cue of how much zoom headroom remains.

Adding a ZoomSlider communicates current scale depth immediately and lets users scrub across the whole zoom range in a single drag while adapting cleanly to your application’s visual theme.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

zoomslider.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  ControlZoomSliderComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceOsmComponent,
  ViewComponent,
} from 'cc-openlayers';

/** Three maps with a zoom slider: the default one, one between the zoom buttons and a horizontal restyled one. */
@Component({
  selector: 'app-zoomslider',
  imports: [
    ControlZoomSliderComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    MapComponent,
    SourceOsmComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    @for (variant of variants; track variant.title) {
      <h4>{{ variant.title }}</h4>
      <div [class]="variant.class">
        <aol-map height="480px">
          <aol-interaction-default />
          <aol-control-defaults />
          <aol-control-zoomslider />

          <aol-view [zoom]="13">
            <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
          </aol-view>
          <aol-layer-tile><aol-source-osm /></aol-layer-tile>
        </aol-map>
      </div>
    }
  `,
})
export class Zoomslider {
  protected readonly variants = [
    { title: 'Default style', class: 'zoomslider-default' },
    { title: 'Placed between zoom controls', class: 'zoomslider-between' },
    { title: 'Horizontal and completely re-styled', class: 'zoomslider-horizontal' },
  ];
}

Architecture and OpenLayers mechanics

Each map mounts an identical aol-control-zoomslider inside a wrapper carrying zoomslider-default, zoomslider-between or zoomslider-horizontal. OpenLayers inspects the rendered CSS dimensions of the slider element on first paint: if its computed box is taller than wide it operates vertically, whereas making it wider than tall in CSS (zoomslider-horizontal) automatically switches thumb drag math to horizontal.

By default the track spans the view’s full zoom domain (0 to 28 unless constrained), positioning zoom 13 just below the midpoint.

How it works

  • aol-control-zoomslider mounts a draggable zoom thumb whose track clicks animate the view to the chosen scale over 200 ms.
  • Wrapper classes zoomslider-default, zoomslider-between and zoomslider-horizontal reposition and restyle the control purely through CSS.
  • OpenLayers infers slider orientation from element geometry: styling the track wider than tall switches interaction mechanics from vertical to horizontal.
  • Track position reflects the view’s configured zoom range (0–28 by default), placing city-centre zoom 13 near the middle of the rail.

Pitfalls

  • OpenLayers determines vertical versus horizontal orientation once when the control first renders with non-zero dimensions—flipping aspect ratio later via a responsive media query will not update drag axis handling without recreating the control.
  • Unconstrained views run up to zoom 28 even though OpenStreetMap tiles stop at zoom 19, leaving the upper third of the track overscaling blurry tiles; set maxZoom on the view so the slider spans only real data levels.
  • In horizontal mode OpenLayers places maximum zoom-in on the left end and zoom-out on the right—the opposite of typical media sliders—so add clear icons or tooltips if users expect left-to-right zoom-in.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.