Last active
January 18, 2017 20:19
-
-
Save andy-esch/bf29a9327deb7b8f25c8 to your computer and use it in GitHub Desktop.
Using a slider to control opacity of CartoDB layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<!-- pulled some code from http://jqueryui.com/slider/#rangemin --> | |
<head> | |
<meta charset="utf-8"> | |
<title>CartoDB.js Opacity Slider</title> | |
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css"> | |
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> | |
<script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script> | |
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css"> | |
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/themes/css/cartodb.css" /> | |
<style> | |
#map { | |
padding: 0; | |
margin: 0; | |
height: 67%; | |
width: 100%; | |
} | |
#dash { | |
padding: 25px; | |
margin: 0; | |
height: 33%; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="map"></div> | |
<div id="dash"> | |
<p> | |
<label for="amount">Opacity:</label> | |
<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;"> | |
</p> | |
<div id="header"></div> | |
<div id="slider-range-min"></div> | |
</div> | |
<script src="http://libs.cartocdn.com/cartodb.js/v3/cartodb.js"></script> | |
<script> | |
window.onload = function() { | |
cartodb.createVis('map', 'http://documentation.cartodb.com/api/v2/viz/2b13c956-e7c1-11e2-806b-5404a6a683d5/viz.json') | |
.done(function(vis, layers) { | |
var op = 0.5; | |
layers[1].setOpacity(op); | |
$(function() { | |
$( "#slider-range-min" ).slider({ | |
range: "min", | |
value: 50, | |
min: 0, | |
max: 100, | |
slide: function( event, ui ) { | |
$( "#amount" ).val(ui.value + "%" ); | |
op = $( "#slider-range-min" ).slider( "value" ) / 100; | |
layers[1].setOpacity(op); | |
} | |
}); | |
$( "#amount" ).val( $( "#slider-range-min" ).slider( "value" ) + "%"); | |
}); | |
}); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment