Legacy
Mapbox.js is no longer in active development. To learn more about our newer mapping tools see Mapbox GL JS.
Opacity control
Click and drag a slider to adjust the opacity of an overlay.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Opacity control</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<style>
.ui-opacity {
background:#FFF;
position:absolute;
left:10px;
top:70px;
height:200px;
width:28px;
border:1px solid rgba(0,0,0,0.4);
border-radius:3px;
z-index:1000;
}
.ui-opacity .handle {
position:absolute;
background:#404040;
left:0;
top:20px;
width:26px;
height:10px;
border-radius:1px;
cursor:pointer;
cursor:ns-resize;
}
.ui-opacity .handle:hover {
background:#303030;
}
</style>
<div id='map'></div>
<div id='control' class='ui-opacity'>
<div id='handle' class='handle'></div>
</div>
<script>
L.mapbox.accessToken = '<your access token here>';
var handle = document.getElementById('handle'),
start = false,
startTop;
var map = L.mapbox.map('map')
.setView([43.6654, -79.4775], 14)
.addLayer(L.mapbox.styleLayer('mapbox://styles/mapbox/light-v10'));
var overlay = L.mapbox.tileLayer('aibram.Aerial', {
zIndex: 2
})
.addTo(map);
document.onmousemove = function(e) {
if (!start) return;
// Adjust control.
handle.style.top = Math.max(-5, Math.min(195, startTop + parseInt(e.clientY, 10) - start)) + 'px';
// Adjust opacity.
overlay.setOpacity(1 - (handle.offsetTop / 200));
};
handle.onmousedown = function(e) {
// Record initial positions.
start = parseInt(e.clientY, 10);
startTop = handle.offsetTop - 5;
return false;
};
document.onmouseup = function(e) {
start = null;
};
</script>
Get a free Mapbox account to create your own custom map and use it in this example.
Use this example by copying its source into your own HTML page and
replacing the Map ID
with
one of your own from your projects.
Having trouble with JavaScript? Try out Codecademy
or contact our support team.