最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I change the background color of a Leaflet popup? - Stack Overflow

programmeradmin2浏览0评论

I'm creating a map using Leafletjs, and I'd like to change the background color of a popup (which is currently displaying and image and a link) from white to a different color. It seems that basic background color css syntax won't cut it. Any advice? Thanks, -Scott

I'm creating a map using Leafletjs, and I'd like to change the background color of a popup (which is currently displaying and image and a link) from white to a different color. It seems that basic background color css syntax won't cut it. Any advice? Thanks, -Scott

Share Improve this question asked Dec 12, 2013 at 0:16 ScottScott 1371 gold badge1 silver badge10 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

After you call leaflet.css, you can include a <style> tag with the following rule to change the color of the popup and popup tip.

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background-color: #000
}

Here's a screenshot I took after I edited background-color of a popup on Leaflet's homepage.

Open leaflet.css and search for:

    .leaflet-popup-content-wrapper,
    .leaflet-popup-tip {
    background: rgb(111, 51, 51);
    box-shadow: 0 3px 14px rgba(0,0,0,0.4);
}

Then change the background value to whatever color you want.

      const marker = new L.marker(lastPoint, {
        icon: markerIconSnake
      }).bindPopup(getDataInHtml(dataPopup), {
        className: 'stylePopup'
      })

If you want to change the background color of a popup you can use the method .bindPopup (in your marker) and add a css class.

    .stylePopup .leaflet-popup-content-wrapper,
    .stylePopup .leaflet-popup-tip {
        background-color: #f4913b;
        padding: 8px;
    }

If you wanna know more head to the docs!

In my case I'm using react-leaflet v2 and wasn't able to use css in js with material/core/styles. I created a function

const updatePopupCss = (color) => {
    let popupElement = document.getElementsByClassName("leaflet-popup-content-wrapper");
    let htmlPopupElement;
    if (popupElement[0] instanceof HTMLElement) {
        htmlPopupElement = popupElement[0] as HTMLElement;
        htmlPopupElement.style.backgroundColor = color;
        console.log(htmlPopupElement)
    }
}

Then called it from the onOpen attribute like so

 <Popup onOpen={() => {updatePopupCss("#036597")}} >

发布评论

评论列表(0)

  1. 暂无评论