将可折叠侧边栏添加到地图框地图



我需要在地图框地图中添加一个可折叠的侧边栏。

我正在使用这个例子:https://www.w3schools.com/howto/howto_js_collapse_sidebar.asp

这是我的 html 文件。它显示它短暂加载,但随后地图在其顶部渲染。

最终,我想开发一个侧边栏,就像您单击Google地图中的功能时弹出的那样。我还没有找到这样的东西,所以如果有人知道解决方案,请告诉我。

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Castello Plan: Sidebar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />
	
	
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
	
	
<!-- For Interactive sidebar Head-->
<style>
body {
  font-family: "Lato", sans-serif;
}
.sidebar {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}
.sidebar a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}
.sidebar a:hover {
  color: #f1f1f1;
}
.sidebar .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}
.openbtn {
  font-size: 20px;
  cursor: pointer;
  background-color: #111;
  color: white;
  padding: 10px 15px;
  border: none;
}
.openbtn:hover {
  background-color: #444;
}
#main {
  transition: margin-left .5s;
  padding: 16px;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
  .sidebar {padding-top: 15px;}
  .sidebar a {font-size: 18px;}
}
</style>
	
<!--End Interactive Sidebar Head-->
	
</head>

<body>
<div id='map'></div>
<!--For Interactive Sidebar Body-->
<div id="mySidebar" class="sidebar">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
<div id="main">
  <button class="openbtn" onclick="openNav()">☰ Toggle Sidebar</button>  
  <h2>Collapsed Sidebar</h2>
  <p>Click on the hamburger menu/bar icon to open the sidebar, and push this content to the right.</p>
</div>
<script>
function openNav() {
  document.getElementById("mySidebar").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft= "0";
}
</script>
<!--End Interactive Sidebar Body-->
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoibml0dHlqZWUiLCJhIjoid1RmLXpycyJ9.NFk875-Fe6hoRCkGciG8yQ';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/nittyjee/cjowjzrig5pje2rmmnjb5b0y2',
    center: [0, 0],
    zoom: 0
});
</script>
</body>
</html>

首先,将你的地图添加到主div,并将地图 css 设置为 "position:relative;"您的打开按钮也需要是"位置:绝对",并且在地图上方显示 z 深度为 1。

我为html,body和map css添加了100%的高度,以确保内容拉伸到全高。

希望这有帮助。

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Castello Plan: Sidebar</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />
	
	
<style>
 body { margin:0; padding:0; }
        #map { 
        position: absolute; 
        top: 0; right: 0; 
        bottom: 0; left: 0;
        width:100%;
        height:100%; 
        }
   
html, body, #map{
    height:100%;
    margin: 0px;
}
.sidebar {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}
.sidebar a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}
.sidebar a:hover {
  color: #f1f1f1;
}
.sidebar .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}
.openbtn {
  font-size: 20px;
  cursor: pointer;
  background-color: #111;
  color: white;
  padding: 10px 15px;
  border: none;    
  position:absolute; 
  top:10px; 
  left:10px; 
  z-index:1;
  border-radius: 3px;
}
.openbtn:hover {
  background-color: #444;
}
#main {
  position:relative;
  transition: margin-left .5s;
  padding: 16px;
  height:100%;
}
/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
  .sidebar {padding-top: 15px;}
  .sidebar a {font-size: 18px;}
}   
</style>
	
	
</head>

<body>
<!--For Interactive Sidebar Body-->
<div id="mySidebar" class="sidebar">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
<div id="main">
  <button class="openbtn" onclick="openNav()">☰ Toggle Sidebar</button>  
 <div id='map'></div>
</div>
<script>
function openNav() {
  document.getElementById("mySidebar").style.width = "250px";
  document.getElementById("main").style.marginLeft = "250px";
}
function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft= "0";
}
</script>
<!--End Interactive Sidebar Body-->
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoibml0dHlqZWUiLCJhIjoid1RmLXpycyJ9.NFk875-Fe6hoRCkGciG8yQ';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/nittyjee/cjowjzrig5pje2rmmnjb5b0y2',
    center: [0, 0],
    zoom: 0
});
</script>
</body>
</html>

最新更新