【发布时间】:2019-11-29 15:04:37
【问题描述】:
我正在使用 AngularJS、jQuery UI(带有用于调整大小的自定义指令)和 Bootstrap。在我的页面上,谷歌地图出现在顶部,表格将出现在底部。当用户调整底部的大小时,我希望谷歌地图自动调整大小以占用剩余空间。
我尝试了几种不同的方法,包括:
- 设置
html, body { height: 100%; width: 100% }和#mapCanvas { height: 100% }。 - 使用 flexbox 和 flexgrow。
- 在底部调整大小后计算顶部的大小,并在此基础上动态更改
#mapCanvas元素的高度。
但我没有运气。任何建议,将不胜感激。下面是一个演示该问题的示例。
JSFiddle(如果 sn-p 区域被 Google Maps API 密钥警告遮挡):https://jsfiddle.net/jmg157/9odt0hLn/
function initMap() {
let mapOptions = {
zoom: 8,
minZoom: 8,
center: new google.maps.LatLng(43, -81),
mapTypeId: google.maps.MapTypeId.ROADMAP,
};
map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
}
let app = angular.module("mapApp", ['ui.bootstrap']);
app.directive('resizable', function() {
return {
restrict: 'A',
scope: {
callback: '&onResize'
},
link: function postLink(scope, elem, attrs) {
elem.resizable({
"handles": {
"n": ".ui-resizable-n"
}
});
}
};
});
app.controller("MapAppController", function($scope) {
initMap();
});
html,
body {
height: 100%;
width: 100%;
}
#mapCanvas {
/*height: 100%;*/
height: 60vh;
}
.ui-resizable-n {
border-top: 5px solid grey;
}
.table {
background: #fff;
}
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key="></script>
</head>
<body ng-app="mapApp">
<div ng-controller="MapAppController">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div id="mapCanvas"></div>
</div>
</div>
<div class="row" resizable>
<div class="ui-resizable-n ui-resizable-handle"></div>
<div class="col-md-12 col-xs-12">
Bottom Content Section
</div>
</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
您可以使用
resize事件并根据可调整大小的元素高度设置地图容器高度(在这种情况下,顶部可调整大小...)。 jsfiddle.net/upsidown/dgsz4L0e -
@MrUpsidown 完美!这个解决方案效果很好。我只需要稍微调整计算以考虑页面顶部的 div。如果您想发表评论作为答案,我很乐意接受。
标签: javascript angularjs google-maps-api-3 twitter-bootstrap-3