Getting started (1)Display a mapStyles (11)Add a default markerAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapDisplay a map with a custom styleRender world copiesDisplay a satellite mapChange a map's styleDisplay a mapLayers (40)Display buildings in 3DExtrude polygons for 3D indoor mappingAdd a 3D modelAdd an animated icon to the mapAdd a generated icon to the mapGenerate and add a missing icon to the mapAdd a stretchable image to the mapAdd an icon to the mapAdjust a layer's opacityAnimate a lineAnimate a series of imagesAnimate a pointChange building color based on zoom levelChange the case of labelsDisplay HTML clusters with custom propertiesCreate and style clustersChange a layer's color with buttonsAdd a custom style layerStyle circles with a data-driven propertyStyle lines with a data-driven propertyDisplay and style rich text labelsAdd a pattern to a polygonAdd a new layer below labelsAdd a GeoJSON lineDraw GeoJSON pointsAdd a GeoJSON polygonCreate a heatmap layerAdd hillshadingAdd an imageDisplay line that crosses 180th meridianCreate a gradient line using an expressionAdd multiple geometries from one GeoJSON sourceStyle ocean depth dataShow and hide layersChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelVariable label placementAdd a videoVisualize population densityUse a fallback imageSources (16)Animate a lineAnimate a series of imagesAnimate a pointAdd a canvas sourceJoin local JSON data with vector tile geometriesAdd hillshadingAdd an imageDisplay line that crosses 180th meridianAdd live realtime dataUpdate a feature in realtimeAdd a raster tile sourceAdd a third party vector tile sourceCreate a time sliderAdd a vector tile sourceAdd a videoAdd a WMS sourceUser interaction (27)Adjust a layer's opacityCustomize camera animationsCenter the map on a clicked symbolChange a layer's color with buttonsAnimate 3D buildings based on ambient soundsDisable map rotationCreate a draggable MarkerCreate a draggable pointFilter features within map viewFilter symbols by text inputFilter symbols by toggling a listCreate a hover effectDisplay a non-interactive mapChange a map's languageMeasure distancesGet coordinates of the mouse pointerHighlight features containing similar dataSelect features around a clicked pointGet features under the mouse pointerRestrict map panning to an areaChange a map's styleCreate a time sliderToggle interactionsChange worldview of administrative boundariesUpdate a choropleth layer by zoom levelHighlight features within a bounding boxFit to the bounds of a LineStringCamera (13)Animate map camera around a pointAnimate a point along a routeCustomize camera animationsCenter the map on a clicked symbolFit a map to a bounding boxSlowly fly to a locationFly to a locationNavigate the map with game-like controlsJump to a series of locationsOffset the vanishing point using paddingPlay map locations as a slideshowFly to a location based on scroll positionSet pitch and bearingControls and overlays (17)Animate a markerChange the default position for attributionAdd custom icons with MarkersDisable scroll zoomView a fullscreen mapLocate the userSwipe between mapsDisplay driving directionsShow drawn polygon areaAdd a geocoderAdd a marker using a place nameDisplay map navigation controlsShow polygon information on clickDisplay a popup on clickDisplay a popup on hoverDisplay a popupAttach a popup to a marker instanceGeocoder (10)Supplement forward geocoding search results from another data sourceAccept coordinates as input to a geocoderLimit geocoder results to a named regionUse a custom render function with a geocoderUse the geocoder without a mapPlace the geocoder input outside the mapUse a custom camera animation with a geocoderLocalize the geocoder to a given languageAdd a geocoderSet a point after Geocoder resultBrowser support (1)Check if Mapbox GL JS is supportedInternationalization support (2)Use locally generated ideographsAdd support for right-to-left scripts

Fly to a location based on scroll position

Scroll down through the story and the map will fly to the chapter's location.

<!DOCTYPE html>
<meta charset="utf-8" />
<title>Fly to a location based on scroll position</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src=""></script>
<link href="" rel="stylesheet" />
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#map {
position: fixed;
width: 50%;
#features {
width: 50%;
margin-left: 50%;
font-family: sans-serif;
overflow-y: scroll;
background-color: #fafafa;
section {
padding: 25px 50px;
line-height: 25px;
border-bottom: 1px solid #ddd;
opacity: 0.25;
font-size: 13px;
} {
opacity: 1;
section:last-child {
border-bottom: none;
margin-bottom: 200px;
<div id="map"></div>
<div id="features">
<section id="baker" class="active">
<h3>221b Baker St.</h3>
November 1895. London is shrouded in fog and Sherlock Holmes and
Watson pass time restlessly awaiting a new case. "The London
criminal is certainly a dull fellow," Sherlock bemoans. "There have
been numerous petty thefts," Watson offers in response. Just then a
telegram arrives from Sherlock's brother Mycroft with a mysterious
<section id="aldgate">
<h3>Aldgate Station</h3>
Arthur Cadogan West was found dead, head crushed in on train tracks
at Aldgate Station at 6AM Tuesday morning. West worked at Woolwich
Arsenal on the Bruce-Partington submarine, a secret military
project. Plans for the submarine had been stolen and seven of the
ten missing papers were found in West's possession. Mycroft implores
Sherlock to take the case and recover the three missing papers.
<section id="london-bridge">
<h3>London Bridge</h3>
Holmes and Watson's investigations take them across London. Sherlock
deduces that West was murdered elsewhere, then moved to Aldgate
Station to create the illusion that he was crushed on the tracks by
a train. On their way to Woolwich Sherlock dispatches a telegram to
Mycroft at London Bridge: "Send list of all foreign spies known to
be in England, with full address."
<section id="woolwich">
<h3>Woolwich Arsenal</h3>
While investigating at Woolwich Arsenal Sherlock learns that West
did not have the three keys&mdash;door, office, and
safe&mdash;necessary to steal the papers. The train station clerk
mentions seeing an agitated West boarding the 8:15 train to London
Bridge. Sherlock suspects West of following someone who had access
to the Woolwich chief's keyring with all three keys.
<section id="gloucester">
<h3>Gloucester Road</h3>
Mycroft responds to Sherlock's telegram and mentions several spies.
Hugo Oberstein of 13 Caulfield Gardens catches Sherlock's eye. He
heads to the nearby Gloucester Road station to investigate and
learns that the windows of Caulfield Gardens open over rail tracks
where trains stop frequently.
<section id="caulfield-gardens">
<h3>13 Caulfield Gardens</h3>
Holmes deduces that the murderer placed West atop a stopped train at
Caulfield Gardens. The train traveled to Aldgate Station before
West's body finally toppled off. Backtracking to the criminal's
apartment, Holmes finds a series of classified ads from
<em>The Daily Telegraph</em> stashed away. All are under the name
Pierrot: "Monday night after nine. Two taps. Only ourselves. Do not
be so suspicious. Payment in hard cash when goods delivered."
<section id="telegraph">
<h3>The Daily Telegraph</h3>
Holmes and Watson head to The Daily Telegraph and place an ad to
draw out the criminal. It reads: "To-night. Same hour. Same place.
Two taps. Most vitally important. Your own safety at stake.
Pierrot." The trap works and Holmes catches the criminal: Colonel
Valentine Walter, the brother of Woolwich Arsenal's chief. He
confesses to working for Hugo Oberstein to obtain the submarine
plans in order to pay off his debts.
<section id="charing-cross">
<h3>Charing Cross Hotel</h3>
Walter writes to Oberstein and convinces him to meet in the smoking
room of the Charing Cross Hotel where he promises additional plans
for the submarine in exchange for money. The plan works and Holmes
and Watson catch both criminals.
<small id="citation">
Adapted from
<a href=""
>Project Gutenberg</a
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-0.15591514, 51.51830379],
zoom: 15.5,
bearing: 27,
pitch: 45
var chapters = {
'baker': {
bearing: 27,
center: [-0.15591514, 51.51830379],
zoom: 15.5,
pitch: 20
'aldgate': {
duration: 6000,
center: [-0.07571203, 51.51424049],
bearing: 150,
zoom: 15,
pitch: 0
'london-bridge': {
bearing: 90,
center: [-0.08533793, 51.50438536],
zoom: 13,
speed: 0.6,
pitch: 40
'woolwich': {
bearing: 90,
center: [0.05991101, 51.48752939],
zoom: 12.3
'gloucester': {
bearing: 45,
center: [-0.18335806, 51.49439521],
zoom: 15.3,
pitch: 20,
speed: 0.5
'caulfield-gardens': {
bearing: 180,
center: [-0.19684993, 51.5033856],
zoom: 12.3
'telegraph': {
bearing: 90,
center: [-0.10669358, 51.51433123],
zoom: 17.3,
pitch: 40
'charing-cross': {
bearing: 90,
center: [-0.12416858, 51.50779757],
zoom: 14.3,
pitch: 20
// On every scroll event, check which element is on screen
window.onscroll = function () {
var chapterNames = Object.keys(chapters);
for (var i = 0; i < chapterNames.length; i++) {
var chapterName = chapterNames[i];
if (isElementOnScreen(chapterName)) {
var activeChapterName = 'baker';
function setActiveChapter(chapterName) {
if (chapterName === activeChapterName) return;
document.getElementById(chapterName).setAttribute('class', 'active');
document.getElementById(activeChapterName).setAttribute('class', '');
activeChapterName = chapterName;
function isElementOnScreen(id) {
var element = document.getElementById(id);
var bounds = element.getBoundingClientRect();
return < window.innerHeight && bounds.bottom > 0;
Was this example helpful?