-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.js
92 lines (75 loc) · 3.71 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
var imageContainerMargin = 70; // Margin + padding
// This watches for the scrollable container
var scrollPosition = 0;
$('div#contents').scroll(function() {
scrollPosition = $(this).scrollTop();
});
function initMap() {
// This creates the Leaflet map with a generic start point, because code at bottom automatically fits bounds to all markers
var map = L.map('map', {
center: [0, 0],
zoom: 5,
scrollWheelZoom: false
});
// This displays a base layer map (other options available)
var lightAll = new L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
// This customizes link to view source code; add your own GitHub repository
map.attributionControl
.setPrefix('View <a href="http://github.com/jackdougherty/leaflet-storymap" target="_blank">code on GitHub</a>, created with <a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>');
// This loads the GeoJSON map data file from a local folder
$.getJSON('map.geojson', function(data) {
var geojson = L.geoJson(data, {
onEachFeature: function (feature, layer) {
(function(layer, properties) {
// This creates numerical icons to match the ID numbers
// OR remove the next 6 lines for default blue Leaflet markers
var numericMarker = L.ExtraMarkers.icon({
icon: 'fa-number',
number: feature.properties['id'],
markerColor: 'blue'
});
layer.setIcon(numericMarker);
// This creates the contents of each chapter from the GeoJSON data. Unwanted items can be removed, and new ones can be added
var containerSource = $("#container-template").html();
var containerTemplate = Handlebars.compile(containerSource);
var output = {
"containerId": 'container' + feature.properties['id'],
"chapter": feature.properties['chapter'],
"imgSrc": feature.properties['image'],
"srcHref": feature.properties['source-link'],
"srcText": feature.properties['source-credit'],
"description": feature.properties['description']
}
var html = containerTemplate(output);
$('#contents').append(html);
var i;
var areaTop = -100;
var areaBottom = 0;
// Calculating total height of blocks above active
for (i = 1; i < feature.properties['id']; i++) {
areaTop += $('div#container' + i).height() + imageContainerMargin;
}
areaBottom = areaTop + $('div#container' + feature.properties['id']).height();
$('div#contents').scroll(function() {
if ($(this).scrollTop() >= areaTop && $(this).scrollTop() < areaBottom) {
$('.image-container').removeClass("inFocus").addClass("outFocus");
$('div#container' + feature.properties['id']).addClass("inFocus").removeClass("outFocus");
map.flyTo([feature.geometry.coordinates[1], feature.geometry.coordinates[0] ], feature.properties['zoom']);
}
});
// Make markers clickable
layer.on('click', function() {
$("div#contents").animate({scrollTop: areaTop + "px"});
});
})(layer, feature.properties);
}
});
$('div#container1').addClass("inFocus");
$('#contents').append("<div class='space-at-the-bottom'><a href='#space-at-the-top'><i class='fa fa-chevron-up'></i></br><small>Top</small></a></div>");
map.fitBounds(geojson.getBounds());
geojson.addTo(map);
});
}
initMap();