【发布时间】:2019-01-23 17:27:37
【问题描述】:
我正在使用 Mapboxgl、Bootstrap 4 和 React 渲染地图。
我需要地图占据页面高度的 100% 并显示在两列网格的第一列中。
但是,当使用 React 时,地图的宽度会延伸到行宽的 100% - 在第二列下方重叠。
最好的办法是查看我在 jsfidle 上的示例以理解我的意思。
地图正确显示(使用纯 HTML 且没有 React 时) https://jsfiddle.net/apphancer/jhxy5c63/
显示宽度的地图问题(使用 React 时) https://jsfiddle.net/apphancer/9g71ovn6/
为了让 100% 的高度正常工作,我使用了这个 CSS:
.map-wrapper {
position: fixed;
width: 100%;
height: 100%;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
}
我怀疑这可能与使用 React 渲染地图的方式有关,因为使用纯 HTML 解决方案时不会发生此问题。
有人能指出正确的方向吗?
HTML
<div id="app"></div>
CSS
body, html {
height: 100%;
}
#app, .row, .col-9 {
height: 100%;
}
.col-3 {
background-color: rgba(0, 0, 0, 0.5);
border: 1px solid red;
}
.map-wrapper {
position: fixed;
width: 100%;
height: 100%;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
}
JS
mapboxgl.accessToken = 'pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA';
class Application extends React.Component {
componentDidMount() {
const map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/light-v9', // stylesheet location
center: [13.392, 52.523], // starting position [lng, lat]
zoom: 9 // starting zoom
});
map.addControl(new mapboxgl.NavigationControl());
map.resize(); // tried with this to see if it would help
}
render() {
return (
<div className="row no-gutters">
<div className="col-9">
<div className="map-wrapper">
<div ref={el => this.mapContainer = el} id="map"/>
</div>
</div>
<div className="col-3">
2 of 2
</div>
</div>
)
}
}
ReactDOM.render(<Application/>, document.getElementById('app'));
【问题讨论】:
标签: css reactjs bootstrap-4 mapbox mapbox-gl-js