問題描述
我目前正在使用 Leaflet 和 Mapbox 創(chuàng)建自定義地圖.在我開始處理彈出窗口之前,一切都運(yùn)行良好.
我的情況:我有一個(gè)將要使用的自定義導(dǎo)航/控制面板,但它需要在任何打開的彈出窗口之后(目前不是).
我準(zhǔn)備了一個(gè)
我的 CSS 的一部分是
默認(rèn)傳單類的 z-index
為 7(我認(rèn)為),我只是將其重寫為 100% 確定.在瀏覽器(FF/Chrome)中檢查代碼時(shí),我看到 z-index 設(shè)置正確,并且 .leaflet-popup
中的其他元素沒有任何 z-index
設(shè)置.
我已經(jīng)讀到否定的 z-index
可以解決這個(gè)問題,但是當(dāng)我使用多個(gè)元素時(shí),我真的很喜歡一個(gè)不那么hacky"的解決方案——如果有的話.(也歡迎使用 JavaScript 解決方案,因此使用標(biāo)簽)
從代碼檢查中我可以看到,Leaflet 為 .leaflet-popup
設(shè)置了一些屬性,這些屬性在位置方面是:transform: translate3d(..)
,bottom
和 left
.
感謝任何幫助.
我可以把 #map-nav
放在 #map-content
里面,但還是不行):
- 將
#map-nav
移動到.leaflet-map-pane
- 創(chuàng)建一個(gè)
MutationObserver
并觀察.leaflet-map-pane
的屬性變化. - 每當(dāng)
.leaflet-map-pane
的style
屬性發(fā)生變化時(shí),抓取它的style.transform
屬性并提取 x 和 y 值.反轉(zhuǎn)這些值并將它們應(yīng)用到#map-nav
.
這可能會在拖動時(shí)在某些客戶端上滯后.您可以使用超時(shí)來推遲更新,但這意味著導(dǎo)航菜單會四處移動,直到您停止拖動.
I am currently working with Leaflet and Mapbox to create a custom map. All has been working fine until I started to work on the popups.
My situation: I have a custom navigation/control panel that will be used but it needs to be behind any open popups (which it currently is not).
I have prepared a JSFiddle and this screenshot
Part of my CSS is
The default leaflet classes have z-index
of 7 (I think), I just overwrote it to be 100% sure. When inspecting the code in browser (FF/Chrome), I see that the z-index is set properly and no other element in the .leaflet-popup
has any z-index
set.
I have read that negative z-index
may solve this, but as I am working with multiple elements I'd really much like a less "hacky" solution - if there is one. (JavaScript solutions are welcome as well, hence the tag)
As I can see from the code inspection, Leaflet sets some attributes to .leaflet-popup
which in terms of position are: transform: translate3d(..)
, bottom
and left
.
Any help is appreciated.
Edit:
I can put the #map-nav
inside #map-content
and it still won't work fiddle. However, when I try to imitate this without alle the map stuff, it does work.
The problem is that #map-nav
exists in an entirely different stacking context than the marker and its popup.
The way you have it set up right now, the entire map is behind #map-nav
. So, changing the z-index on either #map-nav
or the .leaflet-popup-pane
won't really help.
Disclaimer: This is a terrible, terrible hack. If you look into Leaflet's documentation and find a way to add your own widgets, I'd highly recommend doing that instead of this.
That said, I have something which sort of works (your mileage may vary):
- Move
#map-nav
into.leaflet-map-pane
- Create a
MutationObserver
and observe changes to the attributes of.leaflet-map-pane
. - Whenever the
style
attribute of.leaflet-map-pane
changes, grab itsstyle.transform
property and extract the x and y values. Invert those values and apply them to#map-nav
.
This may lag on some clients while dragging. You could use a timeout to defer the update but that would mean the nav menu would move around until you stop dragging.
這篇關(guān)于z-index 未按預(yù)期工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!