地图显示后按钮不出现

时间:2018-08-17 04:18:16

标签: css

我有一个带有地图和一些按钮的代码。我想使地图覆盖整个屏幕宽度。地图覆盖了整个宽度,但是按钮应该出现在地图下方,而当前并非如此。

有人可以告诉我我做错了什么吗?

var myloc = new L.LatLng(13.7433242, 100.5421583);
var map = L.map('map').setView(myloc, 12);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  maxZoom: 18,
  id: 'mapbox.streets',
  accessToken: 'pk.eyJ1Ijoic2FuaW5kcmEiLCJhIjoiY2pqam5qZzZnMHRycTNrbWR3ZGF4Mmd5eSJ9.ZRIVhopMmACa80OQ0yZN3g'
}).addTo(map);
.container {
  width: 100%;
}

#map {
  height: 80%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body>
  <div class="container">
    <div class="row">
      <div id="map"></div>
    </div>

    <div class="row">
      <input type="button" id="btn-first" class="btn-a" value="First">
      <input type="button" id="btn-second" class="btn-a" value="Second">
    </div>

  </div>
</body>

4 个答案:

答案 0 :(得分:1)

#map {
height: 200px;
width: 100%;
position: relative;
clear: both;
}

这很好用。.

答案 1 :(得分:1)

您在这里! 这可能对您有帮助。

我在按钮上添加了一个类,并将其设置为position:absolute; + bottom:0;,这使它们不起作用。希望对您有帮助。

var myloc = new L.LatLng(13.7433242, 100.5421583);
var map = L.map('map').setView(myloc, 12);

L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
  maxZoom: 18,
  id: 'mapbox.streets',
  accessToken: 'pk.eyJ1Ijoic2FuaW5kcmEiLCJhIjoiY2pqam5qZzZnMHRycTNrbWR3ZGF4Mmd5eSJ9.ZRIVhopMmACa80OQ0yZN3g'
}).addTo(map);
.container {
  width: 100%;
}

#map {
  height: 80%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  clear: both;
}
 /*this is what I've added*/
.btns{bottom:0; position:absolute;}
 /*this is what I've added*/ 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<body>
  <div class="container">
    <div class="row">
      <div id="map"></div>
    </div>

    <div class="row btns">
      <input type="button" id="btn-first" class="btn-a" value="First">
      <input type="button" id="btn-second" class="btn-a" value="Second">
    </div>

  </div>
</body>

答案 2 :(得分:1)

尝试一下:

.row:nth-child(2) {
    margin-top: 80%;
}

答案 3 :(得分:-1)

根据您的评论更新的代码“是的,我尝试添加z-index。但是该按钮显示在地图顶部。我需要在地图下方显示它们”

jsfiddle.net/1bwmyhvd/4

HTML:-

<div class="container">
<div class="row map-block">
  <div id="map"></div>
</div>

<div class="row">
  <input type="button" id="btn-first" class="btn-a" value="First">
  <input type="button" id="btn-second" class="btn-a" value="Second">
</div>

为地图父行添加了额外的类。

CSS:-

.map-block{
   position:relative;
   min-height:200px;
}

#map {
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  position: absolute;
  clear: both;
  z-index: -1;
}