Skip to content

Instantly share code, notes, and snippets.

@tristen
Created September 30, 2014 14:24
Show Gist options
  • Save tristen/0bd05279a9cb5c77060c to your computer and use it in GitHub Desktop.
Save tristen/0bd05279a9cb5c77060c to your computer and use it in GitHub Desktop.
Hover-driven navigation with markers and a narrative
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Hover-driven navigation with markers and a narrative</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox.js/v2.1.2/mapbox.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox.js/v2.1.2/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
article {
position:absolute;
top:0;
right:0;
bottom:0px;
left:0;
overflow:auto;
}
.quiet {
color:rgba(0,0,0,0.5);
}
.scroll {
display:block;
text-align:center;
}
.sections {
background:rgba(255,255,255,0.5);
width:240px;
}
section {
padding:20px;
color:rgba(0,0,0,0.5);
-webkit-transition:background 500ms, color 500ms;
transition:background 500ms, color 500ms;
}
section:hover,
section.active {
background:#fff;
color:#404040;
}
</style>
<div id='map'></div>
<article id='narrative'>
<div class='sections prose'>
<section id='cover' class='cover active'>
<h2>The Adventure of the Bruce-Partington Plans</h2>
<p>by Sir Arthur Conan Doyle</p>
</section>
<section id='baker'>
<h3>221b Baker St.</h3>
<p>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 case.</p>
</section>
<section id='aldgate'>
<h3>Aldgate Station</h3>
<p>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.</p>
</section>
<section id='london-bridge'>
<h3>London Bridge</h3>
<p>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."</p>
</section>
<section id='woolwich'>
<h3>Woolwich Arsenal</h3>
<p>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.</p>
</section>
<section id='gloucester'>
<h3>Gloucester Road</h3>
<p>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.</p>
</section>
<section id='caulfield-gardens'>
<h3>13 Caulfield Gardens</h3>
<p>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."</p>
</section>
<section id='telegraph'>
<h3>The Daily Telegraph</h3>
<p>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.</p>
</section>
<section id='charing-cross'>
<h3>Charing Cross Hotel</h3>
<p>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.</p>
<small class='colophon'>
Adapted from <a href='http://www.gutenberg.org/files/2346/2346-h/2346-h.htm'>Project Gutenberg</a>
</small>
</section>
</div>
</article>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoidHJpc3RlbiIsImEiOiJiUzBYOEJzIn0.VyXs9qNWgTfABLzSI3YcrQ';
// In this case, we just hardcode data into the file. This could be dynamic.
// The important part about this data is that the 'id' property matches
// the HTML above - that's how we figure out how to link up the
// map and the data.
var places = { type: 'FeatureCollection', features: [
{ geometry: { type: "Point", coordinates: [-0.12960000, 51.50110000] },
properties: { id: "cover", zoom: 9 }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.15591514, 51.51830379] },
properties: { id: "baker" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.07571203, 51.51424049] },
properties: { id: "aldgate" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.08533793, 51.50438536] },
properties: { id: "london-bridge" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [0.05991101, 51.48752939] },
properties: { id: "woolwich" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.18335806, 51.49439521] },
properties: { id: "gloucester" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.19684993, 51.5033856] },
properties: { id: "caulfield-gardens" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.10669358, 51.51433123] },
properties: { id: "telegraph" }, type: 'Feature' },
{ geometry: { type: "Point", coordinates: [-0.12416858, 51.50779757] },
properties: { id: "charing-cross" }, type: 'Feature' }
]};
var map = L.mapbox.map('map', 'examples.map-i86nkdio', {
zoomControl: false
});
var placesLayer = L.mapbox.featureLayer(places)
.addTo(map);
// Ahead of time, select the elements we'll need -
// the narrative container and the individual sections
var narrative = document.getElementById('narrative'),
sections = narrative.getElementsByTagName('section'),
currentId = '';
setId('cover');
function setId(newId) {
// If the ID hasn't actually changed, don't do anything
if (newId === currentId) return;
// Otherwise, iterate through layers, setting the current
// marker to a different color and zooming to it.
placesLayer.eachLayer(function(layer) {
if (layer.feature.properties.id === newId) {
map.setView(layer.getLatLng(), layer.feature.properties.zoom || 14);
layer.setIcon(L.mapbox.marker.icon({
'marker-color': '#a8f'
}));
} else {
layer.setIcon(L.mapbox.marker.icon({
'marker-color': '#404040'
}));
}
});
// highlight the current section
for (var i = 0; i < sections.length; i++) {
sections[i].className = sections[i].id === newId ? 'active' : '';
}
// And then set the new id as the current one,
// so that we know to do nothing at the beginning
// of this function if it hasn't changed between calls
currentId = newId;
}
// Loop through each section and attach
// a hover event to it.
var current;
for(var i = 0; i < sections.length; i++){
sections[i].addEventListener('mouseover', function() {
if (this.id !== current) {
setId(this.id)
current = this.id;
}
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment