---
title: "Marker animations, transitions, and interactive UI states | JavaScript maps SDK"
description: "Create animated markers with interactive UI states and transitions."
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs-qmgv.pages.dev/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Marker animations, transitions, and interactive UI states

This example demonstrates how to configure lifecycle animations, property transitions, and interactive UI states on **MapTiler Marker** in SDK v5:

-   **Interactive UI states:** Declarative styling for `hover` (scale up and shadow), `focus` (custom outline on click or Tab navigation), `active` (shape morphs to square and inner color turns red while pressed), and `dragging` (scale up and inner color change during drag).
-   **Pinned captions:** Labels positioned under each showcase marker using lightweight DOM markers with `pointer-events: none`.
-   **Lifecycle animations:** Smooth marker appearance on addition (`enter: { preset: "drop", duration: 500 }`) and removal (`exit: { preset: "fade", duration: 300 }`).
-   **Property transitions:** Fluid interpolation of properties such as scale and inner color when switching between states (`transitions: { scale: { duration: 250, easing: "ease-out" } }`).
-   **Dynamic controls:** Click **Drop Marker** to drop a new animated marker at the map center, or click **Remove** to trigger the smooth exit animation.

Interact with each marker directly to preview its state, or use the panel controls to drop, remove, and reset markers.

### NPM module setup

```bash
npm install --save @maptiler/sdk
```

#### main.js

```js
import { Map, MapStyle, config, Marker } from "@maptiler/sdk";
import "@maptiler/sdk/dist/maptiler-sdk.css";

config.apiKey = "YOUR_MAPTILER_API_KEY";

const CENTER = [14.42, 50.08];
const SPACING = 0.0042;

const map = new Map({
  container: "map",
  style: MapStyle.STREETS,
  center: CENTER,
  zoom: 15,
});

// Pinned caption under each showcase marker
function addCaption(map, lngLat, text) {
  const captionEl = document.createElement("div");
  captionEl.textContent = text;
  captionEl.style.font = "700 10px system-ui, -apple-system, sans-serif";
  captionEl.style.letterSpacing = "0.03em";
  captionEl.style.color = "rgba(0, 0, 0, 0.75)";
  captionEl.style.background = "rgba(255, 255, 255, 0.85)";
  captionEl.style.padding = "2px 6px";
  captionEl.style.borderRadius = "4px";
  captionEl.style.whiteSpace = "nowrap";
  captionEl.style.pointerEvents = "none";
  captionEl.style.boxShadow = "0 1px 3px rgba(0, 0, 0, 0.1)";

  const caption = new Marker({
    element: captionEl,
    anchor: "top",
    offset: [0, 34],
    htmlAttributes: { tabindex: "-1" },
  });
  caption.setLngLat(lngLat);
  map.addMarker(caption);
  return caption;
}

// Track active markers and captions
let showcaseItems = [];
const dynamicMarkers = [];

function removeMarker(marker) {
  const dynIndex = dynamicMarkers.indexOf(marker);
  if (dynIndex !== -1) {
    dynamicMarkers.splice(dynIndex, 1);
  }

  const showcaseIndex = showcaseItems.findIndex((item) => item.marker === marker);
  if (showcaseIndex !== -1) {
    const [item] = showcaseItems.splice(showcaseIndex, 1);
    if (item.caption) {
      item.caption.remove();
    }
  }

  marker.remove();
}

// Initial showcase markers demonstrating core UI states
const showcaseDefinitions = [
  {
    caption: "HOVER",
    options: {
      content: "H",
      states: { hover: { scale: [1.35, 1.35], shadow: "strong" } },
    },
  },
  {
    caption: "FOCUS (click or Tab)",
    options: {
      content: "F",
      states: { focus: { outline: true, outlineColor: "#22c55e" } },
    },
  },
  {
    caption: "ACTIVE (press and hold)",
    options: {
      content: "A",
      states: { active: { shape: "square", innerColor: "red" } },
    },
  },
  {
    caption: "DRAGGING",
    options: {
      content: "D",
      draggable: true,
      states: { dragging: { scale: [1.5, 1.5], innerColor: "red" } },
    },
  },
];

function initShowcaseMarkers() {
  showcaseDefinitions.forEach(({ caption, options }, i) => {
    const lngLat = [CENTER[0] + (i - 1.5) * SPACING, CENTER[1]];

    const marker = new Marker({
      shape: "circle",
      size: "l",
      lngLat,
      animations: {
        enter: { preset: "drop", duration: 500 },
        exit: { preset: "fade", duration: 300 },
      },
      transitions: {
        scale: { duration: 250, easing: "ease-out" },
        innerColor: { duration: 200 },
      },
      ...options,
    });

    map.addMarker(marker);
    const captionMarker = addCaption(map, lngLat, caption);

    if (options.draggable) {
      marker.on("drag", () => {
        captionMarker.setLngLat(marker.getLngLat());
      });
    }

    // Double click showcase marker to remove it with exit animation
    marker.getElement().addEventListener("dblclick", (e) => {
      e.stopPropagation();
      removeMarker(marker);
    });

    showcaseItems.push({ marker, caption: captionMarker });
  });
}

initShowcaseMarkers();

// Button: Drop new animated marker at center
document.getElementById("btn-add").addEventListener("click", () => {
  const centerPos = map.getCenter();
  const colors = ["#0070F3", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B"];
  const randomColor = colors[Math.floor(Math.random() * colors.length)];
  const jitterLng = (Math.random() - 0.5) * 0.002;
  const jitterLat = (Math.random() - 0.5) * 0.001;
  const lngLat = [centerPos.lng + jitterLng, centerPos.lat + jitterLat];

  const marker = new Marker({
    shape: "maptiler",
    size: "m",
    innerColor: randomColor,
    outerColor: "#FFFFFF",
    shadow: "medium",
    animations: {
      enter: { preset: "drop", duration: 500 },
      exit: { preset: "fade", duration: 300 },
    },
    states: {
      hover: {
        scale: [1.3, 1.3],
        shadow: "strong",
      },
      active: {
        scale: [1.1, 1.1],
      },
    },
    transitions: {
      scale: { duration: 250, easing: "ease-out" },
      innerColor: { duration: 200 },
    },
  }).setLngLat(lngLat);

  // Click directly on dropped marker to remove it with exit animation
  marker.getElement().addEventListener("click", (e) => {
    e.stopPropagation();
    removeMarker(marker);
  });

  map.addMarker(marker);
  dynamicMarkers.push(marker);
});

// Button: Remove last marker with exit animation
document.getElementById("btn-remove").addEventListener("click", () => {
  if (dynamicMarkers.length > 0) {
    const lastDynamic = dynamicMarkers[dynamicMarkers.length - 1];
    removeMarker(lastDynamic);
  } else if (showcaseItems.length > 0) {
    const lastShowcase = showcaseItems[showcaseItems.length - 1];
    removeMarker(lastShowcase.marker);
  }
});

// Button: Reset showcase markers
document.getElementById("btn-reset").addEventListener("click", () => {
  while (dynamicMarkers.length > 0) {
    const m = dynamicMarkers.pop();
    m.remove();
  }
  while (showcaseItems.length > 0) {
    const item = showcaseItems.pop();
    if (item.caption) {
      item.caption.remove();
    }
    item.marker.remove();
  }
  initShowcaseMarkers();
});
```

#### index.html

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="robots" content="noindex" />
    <title>Marker animations and interactive UI states</title>
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div id="map"></div>
    <div class="panel">
      <strong>Marker Animations &amp; UI States</strong>
      <div class="panel-desc">
        Explore interactive UI states: hover, focus (click or Tab), active (press &amp; hold), and
        dragging. Drop or remove markers to preview enter and exit animations.
      </div>
      <div class="panel-buttons">
        <button id="btn-add">Drop Marker</button>
        <button id="btn-remove" class="secondary">Remove</button>
        <button id="btn-reset" class="secondary">Reset</button>
      </div>
    </div>
    <script type="module" src="./main.js"></script>
  </body>
</html>
```

#### style.css

```css
body {
  margin: 0;
  padding: 0;
}
#map {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}
.panel {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 10;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 12px 14px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  font-family:
    system-ui,
    -apple-system,
    sans-serif;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 280px;
}
.panel-desc {
  color: #475569;
  font-size: 12px;
  line-height: 1.4;
}
.panel-buttons {
  display: flex;
  gap: 6px;
}
.panel button {
  background: #0070f3;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 7px 10px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: background 0.2s;
  flex: 1;
}
.panel button:hover {
  background: #0051b3;
}
.panel button.secondary {
  background: #f1f5f9;
  color: #334155;
  border: 1px solid #cbd5e1;
}
.panel button.secondary:hover {
  background: #e2e8f0;
}
```

### Basic JavaScript setup

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Marker animations and interactive UI states</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <link
      href="https://cdn.maptiler.com/maptiler-sdk-js/v5.0.0/maptiler-sdk.css"
      rel="stylesheet"
    />
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
      }
      .panel {
        position: absolute;
        top: 10px;
        left: 10px;
        z-index: 10;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 8px;
        padding: 12px 14px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        font-family:
          system-ui,
          -apple-system,
          sans-serif;
        font-size: 13px;
        display: flex;
        flex-direction: column;
        gap: 8px;
        max-width: 280px;
      }
      .panel-desc {
        color: #475569;
        font-size: 12px;
        line-height: 1.4;
      }
      .panel-buttons {
        display: flex;
        gap: 6px;
      }
      .panel button {
        background: #0070f3;
        color: #fff;
        border: none;
        border-radius: 4px;
        padding: 7px 10px;
        cursor: pointer;
        font-size: 12px;
        font-weight: 500;
        transition: background 0.2s;
        flex: 1;
      }
      .panel button:hover {
        background: #0051b3;
      }
      .panel button.secondary {
        background: #f1f5f9;
        color: #334155;
        border: 1px solid #cbd5e1;
      }
      .panel button.secondary:hover {
        background: #e2e8f0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <div class="panel">
      <strong>Marker Animations &amp; UI States</strong>
      <div class="panel-desc">
        Explore interactive UI states: hover, focus (click or Tab), active (press &amp; hold), and
        dragging. Drop or remove markers to preview enter and exit animations.
      </div>
      <div class="panel-buttons">
        <button id="btn-add">Drop Marker</button>
        <button id="btn-remove" class="secondary">Remove</button>
        <button id="btn-reset" class="secondary">Reset</button>
      </div>
    </div>

    <script type="module">
      import * as maptilersdk from "https://cdn.maptiler.com/maptiler-sdk-js/v5.0.0/maptiler-sdk.min.mjs";

      maptilersdk.config.apiKey = "YOUR_MAPTILER_API_KEY";

      const CENTER = [14.42, 50.08];
      const SPACING = 0.0042;

      const map = new maptilersdk.Map({
        container: "map",
        style: maptilersdk.MapStyle.STREETS,
        center: CENTER,
        zoom: 15,
      });

      // Pinned caption under each showcase marker
      function addCaption(map, lngLat, text) {
        const captionEl = document.createElement("div");
        captionEl.textContent = text;
        captionEl.style.font = "700 10px system-ui, -apple-system, sans-serif";
        captionEl.style.letterSpacing = "0.03em";
        captionEl.style.color = "rgba(0, 0, 0, 0.75)";
        captionEl.style.background = "rgba(255, 255, 255, 0.85)";
        captionEl.style.padding = "2px 6px";
        captionEl.style.borderRadius = "4px";
        captionEl.style.whiteSpace = "nowrap";
        captionEl.style.pointerEvents = "none";
        captionEl.style.boxShadow = "0 1px 3px rgba(0, 0, 0, 0.1)";

        const caption = new maptilersdk.Marker({
          element: captionEl,
          anchor: "top",
          offset: [0, 34],
          htmlAttributes: { tabindex: "-1" },
        });
        caption.setLngLat(lngLat);
        map.addMarker(caption);
        return caption;
      }

      // Track active markers and captions
      let showcaseItems = [];
      const dynamicMarkers = [];

      function removeMarker(marker) {
        const dynIndex = dynamicMarkers.indexOf(marker);
        if (dynIndex !== -1) {
          dynamicMarkers.splice(dynIndex, 1);
        }

        const showcaseIndex = showcaseItems.findIndex((item) => item.marker === marker);
        if (showcaseIndex !== -1) {
          const [item] = showcaseItems.splice(showcaseIndex, 1);
          if (item.caption) {
            item.caption.remove();
          }
        }

        marker.remove();
      }

      // Initial showcase markers demonstrating core UI states
      const showcaseDefinitions = [
        {
          caption: "HOVER",
          options: {
            content: "H",
            states: { hover: { scale: [1.35, 1.35], shadow: "strong" } },
          },
        },
        {
          caption: "FOCUS (click or Tab)",
          options: {
            content: "F",
            states: { focus: { outline: true, outlineColor: "#22c55e" } },
          },
        },
        {
          caption: "ACTIVE (press and hold)",
          options: {
            content: "A",
            states: { active: { shape: "square", innerColor: "red" } },
          },
        },
        {
          caption: "DRAGGING",
          options: {
            content: "D",
            draggable: true,
            states: { dragging: { scale: [1.5, 1.5], innerColor: "red" } },
          },
        },
      ];

      function initShowcaseMarkers() {
        showcaseDefinitions.forEach(({ caption, options }, i) => {
          const lngLat = [CENTER[0] + (i - 1.5) * SPACING, CENTER[1]];

          const marker = new maptilersdk.Marker({
            shape: "circle",
            size: "l",
            lngLat,
            animations: {
              enter: { preset: "drop", duration: 500 },
              exit: { preset: "fade", duration: 300 },
            },
            transitions: {
              scale: { duration: 250, easing: "ease-out" },
              innerColor: { duration: 200 },
            },
            ...options,
          });

          map.addMarker(marker);
          const captionMarker = addCaption(map, lngLat, caption);

          if (options.draggable) {
            marker.on("drag", () => {
              captionMarker.setLngLat(marker.getLngLat());
            });
          }

          // Double click showcase marker to remove it with exit animation
          marker.getElement().addEventListener("dblclick", (e) => {
            e.stopPropagation();
            removeMarker(marker);
          });

          showcaseItems.push({ marker, caption: captionMarker });
        });
      }

      initShowcaseMarkers();

      // Button: Drop new animated marker at center
      document.getElementById("btn-add").addEventListener("click", () => {
        const centerPos = map.getCenter();
        const colors = ["#0070F3", "#10B981", "#8B5CF6", "#EC4899", "#F59E0B"];
        const randomColor = colors[Math.floor(Math.random() * colors.length)];
        const jitterLng = (Math.random() - 0.5) * 0.002;
        const jitterLat = (Math.random() - 0.5) * 0.001;
        const lngLat = [centerPos.lng + jitterLng, centerPos.lat + jitterLat];

        const marker = new maptilersdk.Marker({
          shape: "maptiler",
          size: "m",
          innerColor: randomColor,
          outerColor: "#FFFFFF",
          shadow: "medium",
          animations: {
            enter: { preset: "drop", duration: 500 },
            exit: { preset: "fade", duration: 300 },
          },
          states: {
            hover: {
              scale: [1.3, 1.3],
              shadow: "strong",
            },
            active: {
              scale: [1.1, 1.1],
            },
          },
          transitions: {
            scale: { duration: 250, easing: "ease-out" },
            innerColor: { duration: 200 },
          },
        }).setLngLat(lngLat);

        // Click directly on dropped marker to remove it with exit animation
        marker.getElement().addEventListener("click", (e) => {
          e.stopPropagation();
          removeMarker(marker);
        });

        map.addMarker(marker);
        dynamicMarkers.push(marker);
      });

      // Button: Remove last marker with exit animation
      document.getElementById("btn-remove").addEventListener("click", () => {
        if (dynamicMarkers.length > 0) {
          const lastDynamic = dynamicMarkers[dynamicMarkers.length - 1];
          removeMarker(lastDynamic);
        } else if (showcaseItems.length > 0) {
          const lastShowcase = showcaseItems[showcaseItems.length - 1];
          removeMarker(lastShowcase.marker);
        }
      });

      // Button: Reset showcase markers
      document.getElementById("btn-reset").addEventListener("click", () => {
        while (dynamicMarkers.length > 0) {
          const m = dynamicMarkers.pop();
          m.remove();
        }
        while (showcaseItems.length > 0) {
          const item = showcaseItems.pop();
          if (item.caption) {
            item.caption.remove();
          }
          item.marker.remove();
        }
        initShowcaseMarkers();
      });
    </script>
  </body>
</html>
```

## Related examples

### [Marker lifecycle animations](https://docs-qmgv.pages.dev/sdk-js/examples/marker-lifecycle-animations/)

Examples

Configure enter, idle, and exit animations on map markers.

### [Display marker](https://docs-qmgv.pages.dev/sdk-js/examples/default-marker/)

Tutorials

Learn how to add a default marker to your map.

### [Animate a marker](https://docs-qmgv.pages.dev/sdk-js/examples/animate-marker/)

Examples

Animate map marker positions by updating locations each frame.

### [Add custom icons with markers](https://docs-qmgv.pages.dev/sdk-js/examples/custom-marker-icons/)

Examples

Add custom marker icons to your map.

### [Marker shapes and customization](https://docs-qmgv.pages.dev/sdk-js/examples/marker-custom-shapes/)

Examples

Configure and create customizable, draggable markers in real-time with custom shapes, embedded letters, emojis, sizes, colors, and shadows.
