# Change opacity of a TileLayer using range-slider

359
2
07-19-2018 04:13 AM
New Contributor III

Created TileLayer class as

const fl7 = new TileLayer({
url: "***"
});

Added the TileLayer to the basemap as

var map = new Map({
basemap: app.basemap,
layers: [fl7]
});

Created a range-slider as

<div class="slidecontainer">
<input type="range" min="1" max="100" value="50" class="slider" id="myRange">
</div>

Now, how can I change the opacity of the TileLayer using the value obtained from slider?

Tags (3)
1 Solution

Accepted Solutions
by
MVP Honored Contributor

Here's one way to do it, using your slider

``<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">  <title>Intro to MapView - Create a 2D map - 4.8</title>  <style>    html,    body,    #viewDiv {      padding: 0;      margin: 0;      height: 100%;      width: 100%;    }  </style>  <link rel="stylesheet" href="https://js.arcgis.com/4.8/esri/css/main.css">  <script src="https://js.arcgis.com/4.8/"></script>  <script>    require([      "esri/Map",      "esri/views/MapView",      "esri/layers/TileLayer",      "dojo/domReady!"    ], function(Map, MapView, TileLayer) {      var map = new Map({        basemap: "streets"      });      var view = new MapView({        container: "viewDiv",        map: map,        zoom: 4,        center: [15, 65] // longitude, latitude      });      var layer = new TileLayer({        url: "https://services.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer"      });      map.add(layer);      layer.opacity = .5      var slider = document.getElementById("myRange");      slider.oninput = function() {        layer.opacity = this.value/100;      }     });  </script></head><body>  <div id="viewDiv">    <div class="slidecontainer">      <input type="range" min="1" max="100" value="50" class="slider" id="myRange">    </div>  </div></body></html>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``
2 Replies
by
MVP Honored Contributor

Here's one way to do it, using your slider

``<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">  <title>Intro to MapView - Create a 2D map - 4.8</title>  <style>    html,    body,    #viewDiv {      padding: 0;      margin: 0;      height: 100%;      width: 100%;    }  </style>  <link rel="stylesheet" href="https://js.arcgis.com/4.8/esri/css/main.css">  <script src="https://js.arcgis.com/4.8/"></script>  <script>    require([      "esri/Map",      "esri/views/MapView",      "esri/layers/TileLayer",      "dojo/domReady!"    ], function(Map, MapView, TileLayer) {      var map = new Map({        basemap: "streets"      });      var view = new MapView({        container: "viewDiv",        map: map,        zoom: 4,        center: [15, 65] // longitude, latitude      });      var layer = new TileLayer({        url: "https://services.arcgisonline.com/arcgis/rest/services/World_Terrain_Base/MapServer"      });      map.add(layer);      layer.opacity = .5      var slider = document.getElementById("myRange");      slider.oninput = function() {        layer.opacity = this.value/100;      }     });  </script></head><body>  <div id="viewDiv">    <div class="slidecontainer">      <input type="range" min="1" max="100" value="50" class="slider" id="myRange">    </div>  </div></body></html>‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍``
New Contributor III

Yes it works. Thank you Ken Buja