描述框的 Mapbox 地图样式
Mapbox map styling for description box
我是一名正在学习前端技能的新手,我正在设计自己的网站来学习。我正在尝试使用 Mapbox API 实现地图,但我无法获得我在地图中使用的描述框的样式。
地图在底部。现在我已经设法在地图上放置了一个隐藏的描述框。单击地图上的标记时,隐藏的 属性 会被移除,当单击地图时,它会再次添加回来。
map.css
#map {
width:100%;
height: 500px;
}
pre#description{
position: relative;
top: -100px;
left: 20px;
padding:5px 10px;
background: rgba(0,0,0,0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
border-radius: 5px;
max-width: 25%;
overflow-x: hidden;
overflow-y: visible;
}
map.js
L.mapbox.accessToken = 'pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ';
var map = L.mapbox.map('map', 'examples.map-i86nkdio')
.setView([30, 60])
.on('click', function(){
$("#description").addClass('hidden');
});
L.marker([28.612896, 77.177275], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'warehouse',
'marker-color': '#2880CA'
}),
title: 'My Location'
})
.on('click', function(){
$("#description").removeClass('hidden').empty().append('My Location');
})
.addTo(map);
L.marker([51.081482, 10.300311], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'circle-stroked',
'marker-color': '#74DF00'
}),
title: 'Social Accounting System'
})
.on('click', function(){
$("#description").removeClass('hidden')
.empty()
.append('Social Accounting System developed using Laravel + Bootstrap.Project includes different user roles performing a variety of operations such as transferring commodities and selling them on the market');
})
.addTo(map);
L.marker([22.281385, 114.171317], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'circle-stroked',
'marker-color': '#74DF00'
}),
title: 'Social Accounting System'
})
.on('click', function(){
$("#description").removeClass('hidden')
.empty()
.append('AngulAir was a demo project developed to show basic CRUD operations performed on the front end using AngularJS');
})
.addTo(map);
map.fitBounds([
[22.281385, 114.171317],
[51.081482, 10.300311]
]);
问题是我对 CSS 不是很好,我试图将描述框放在右上角的左上角。我已成功将它放在左下角,但每当出现该框时,地图底部也会出现白色 space。
除此之外,盒子上有一个水平滚动条,但我想要一个垂直滚动条。所以我做了 overflow-x: hidden
和 overflow-y: visible
。水平滚动现在关闭,但我需要将文本换行到垂直滚动。
而且我认为我会使用 max-width: 25%
以便它在所有视口上响应。有更好的方法吗?
如何完成这些任务?
为什么不使用默认控制层并创建自定义 L.Control?这样做,定位就全部为你处理好了:
JS:
var MyCustomControl = L.Control.extend({
options: {
// Default control position
position: 'bottomleft'
},
onAdd: function (map) {
// Create a container with classname and return it
return L.DomUtil.create('div', 'my-custom-control');
},
setContent: function (content) {
// Set the innerHTML of the container
this.getContainer().innerHTML = content;
}
});
// Assign to a variable so you can use it later and add it to your map
var myCustomControl = new MyCustomControl().addTo(map);
现在添加一些样式,注意 :empty
伪选择器,它会在容器为空时隐藏它。
CSS:
.my-custom-control {
padding:5px 10px;
background: rgba(0,0,0,0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
border-radius: 5px;
}
.my-custom-control:empty {
display: none;
}
现在您可以在点击处理程序中使用它:
// Set content on a marker click
marker.on('click', function () {
myCustomControl.setContent('My awesome content');
});
// Remove content on map click
map.on('click', function () {
myCustomControl.setContent('');
});
这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/wrlC7AAHLBWsDgzk8PRw?p=preview
我是一名正在学习前端技能的新手,我正在设计自己的网站来学习。我正在尝试使用 Mapbox API 实现地图,但我无法获得我在地图中使用的描述框的样式。
地图在底部。现在我已经设法在地图上放置了一个隐藏的描述框。单击地图上的标记时,隐藏的 属性 会被移除,当单击地图时,它会再次添加回来。
map.css
#map {
width:100%;
height: 500px;
}
pre#description{
position: relative;
top: -100px;
left: 20px;
padding:5px 10px;
background: rgba(0,0,0,0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
border-radius: 5px;
max-width: 25%;
overflow-x: hidden;
overflow-y: visible;
}
map.js
L.mapbox.accessToken = 'pk.eyJ1Ijoicm9oYW4wNzkzIiwiYSI6IjhFeGVzVzgifQ.MQBzoHJmjH19bXDW0b8nKQ';
var map = L.mapbox.map('map', 'examples.map-i86nkdio')
.setView([30, 60])
.on('click', function(){
$("#description").addClass('hidden');
});
L.marker([28.612896, 77.177275], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'warehouse',
'marker-color': '#2880CA'
}),
title: 'My Location'
})
.on('click', function(){
$("#description").removeClass('hidden').empty().append('My Location');
})
.addTo(map);
L.marker([51.081482, 10.300311], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'circle-stroked',
'marker-color': '#74DF00'
}),
title: 'Social Accounting System'
})
.on('click', function(){
$("#description").removeClass('hidden')
.empty()
.append('Social Accounting System developed using Laravel + Bootstrap.Project includes different user roles performing a variety of operations such as transferring commodities and selling them on the market');
})
.addTo(map);
L.marker([22.281385, 114.171317], {
icon: L.mapbox.marker.icon({
'marker-size': 'large',
'marker-symbol': 'circle-stroked',
'marker-color': '#74DF00'
}),
title: 'Social Accounting System'
})
.on('click', function(){
$("#description").removeClass('hidden')
.empty()
.append('AngulAir was a demo project developed to show basic CRUD operations performed on the front end using AngularJS');
})
.addTo(map);
map.fitBounds([
[22.281385, 114.171317],
[51.081482, 10.300311]
]);
问题是我对 CSS 不是很好,我试图将描述框放在右上角的左上角。我已成功将它放在左下角,但每当出现该框时,地图底部也会出现白色 space。
除此之外,盒子上有一个水平滚动条,但我想要一个垂直滚动条。所以我做了 overflow-x: hidden
和 overflow-y: visible
。水平滚动现在关闭,但我需要将文本换行到垂直滚动。
而且我认为我会使用 max-width: 25%
以便它在所有视口上响应。有更好的方法吗?
如何完成这些任务?
为什么不使用默认控制层并创建自定义 L.Control?这样做,定位就全部为你处理好了:
JS:
var MyCustomControl = L.Control.extend({
options: {
// Default control position
position: 'bottomleft'
},
onAdd: function (map) {
// Create a container with classname and return it
return L.DomUtil.create('div', 'my-custom-control');
},
setContent: function (content) {
// Set the innerHTML of the container
this.getContainer().innerHTML = content;
}
});
// Assign to a variable so you can use it later and add it to your map
var myCustomControl = new MyCustomControl().addTo(map);
现在添加一些样式,注意 :empty
伪选择器,它会在容器为空时隐藏它。
CSS:
.my-custom-control {
padding:5px 10px;
background: rgba(0,0,0,0.5);
color: #fff;
font-size: 11px;
line-height: 18px;
border-radius: 5px;
}
.my-custom-control:empty {
display: none;
}
现在您可以在点击处理程序中使用它:
// Set content on a marker click
marker.on('click', function () {
myCustomControl.setContent('My awesome content');
});
// Remove content on map click
map.on('click', function () {
myCustomControl.setContent('');
});
这是一个关于 Plunker 的工作示例:http://plnkr.co/edit/wrlC7AAHLBWsDgzk8PRw?p=preview