I am newbie working on his front end skills and I am designing my own website to learn. I am trying to implement a map using the Mapbox API and I cannot get the styling of the description box I am using in the map.
Link to my website
The map is at the bottom. Now I have managed to place a hidden description box on the map. When a marker on the map is clicked, the hidden property is removed and when the map is clicked it is added back again.
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 modities 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]
]);
The problem is I am not very good with CSS and I am trying to position the description box on the top left of top right. I have successfully placed it on the bottom left corner but whenever the box appears, a white space also appears on the bottom of the map.
Other than this, there was a horizontal scroll on the box but I wanted a vertical scroll. So I made overflow-x: hidden
and overflow-y: visible
. The horizontal scroll is off now but I need to wrap the text to a vertical scroll.
And also I figured that I would use max-width: 25%
so that it is responsive on all view ports. Is there a better way to do this?
How to acplish these tasks?
I am newbie working on his front end skills and I am designing my own website to learn. I am trying to implement a map using the Mapbox API and I cannot get the styling of the description box I am using in the map.
Link to my website
The map is at the bottom. Now I have managed to place a hidden description box on the map. When a marker on the map is clicked, the hidden property is removed and when the map is clicked it is added back again.
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 modities 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]
]);
The problem is I am not very good with CSS and I am trying to position the description box on the top left of top right. I have successfully placed it on the bottom left corner but whenever the box appears, a white space also appears on the bottom of the map.
Other than this, there was a horizontal scroll on the box but I wanted a vertical scroll. So I made overflow-x: hidden
and overflow-y: visible
. The horizontal scroll is off now but I need to wrap the text to a vertical scroll.
And also I figured that I would use max-width: 25%
so that it is responsive on all view ports. Is there a better way to do this?
How to acplish these tasks?
Share Improve this question asked Feb 4, 2015 at 7:59 RohanRohan 13.8k23 gold badges87 silver badges160 bronze badges1 Answer
Reset to default 8Why not use the default control layer and create a custom L.Control? Doing that, the positioning is all taken care of for you:
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);
Now add some styling, take note of the :empty
pseudoselector which will hide the container if it's 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;
}
Now you can use that in your clickhandlers:
// 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('');
});
Here's a working example on Plunker: http://plnkr.co/edit/wrlC7AAHLBWsDgzk8PRw?p=preview