简体   繁体   English

React-Map-GL:如何使悬停标记出现在顶部?

[英]React-Map-GL: How to make hovered marker appear on top?

I have a map with different markers and as much as I am aware the markers are rendered in the order they appear which means marker 3 is on top of marker 1 and 2. I am using React Map Gl with mapblibre-gl and React(typescript) I'm fine with that, but if I hover an marker I want the hovered marker to be on top(when overlaying with another marker).我有一个带有不同标记的 map,据我所知,标记是按照它们出现的顺序呈现的,这意味着标记 3 在标记 1 和 2 之上。我正在使用 React Map Gl 和 mapblibre-gl 和 React(typescript ) 我对此很好,但如果我 hover 一个标记,我希望悬停标记位于顶部(当与另一个标记重叠时)。 I tried z-index but it does not work at all.我试过 z-index 但它根本不起作用。

Maker:制作者:

<Marker latitude={station.lat} longitude={station.lng} key={station.id} >
                        <div className='locDot'>
                            <div className='priceTag'>
                                <span id="brand">{station.brand ? station.brand : 'freie Tankstelle'}</span>
                                <span id="price">{price}</span>
                            </div>
                        </div>
</Marker >

This marker is than pushed in an array and returned as component of many markers.然后将该标记推入数组并作为许多标记的组成部分返回。

CSS: CSS:

.locDot{
    /* position: absolute; */
    height: 12px;
    width: 12px;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    background-color: #1E90FF;
    border: 2px solid #fff;
    border-radius: 100%; 
    /* z-index: 1; */
    
  
    
}

.priceTag{
    /* position: relative; */
    display: flex;
    flex-direction: column;
    background-color: #1E90FF;
    padding: 4px 6px;
    font-size: 1rem;
    font-weight: 400;
    color: white;
    margin-bottom: 18px;
    border-radius: 5px;
    /* line-height: 1rem; */
    z-index: 1;

   
    
}
.priceTag:hover{
    position: absolute;
    border: 5px solid red;
    z-index: 5;
}

Sandox: codesandbox.io/s/vigorous-mahavira-gxk6wj?file=/src/App.tsx Sandox:codesandbox.io/s/vigorous-mahavira-gxk6wj?file=/src/ App.tsx

You need to apply zIndex to marker itself, you can do this by adding className or inline styling.您需要将 zIndex 应用于标记本身,您可以通过添加className或内联样式来实现。

I have updated the codesandbox with solution, basically we will map simple array and return Markers, based on the Marker index we will set active state and then check whats the current activeIndex in Marker inline style.我已经用解决方案更新了 codesandbox,基本上我们将 map 简单数组并返回标记,基于标记索引我们将设置活动 state 然后检查标记内联样式中的当前 activeIndex 是什么。 If you hover over that div, set an activeIndex to its index..如果你 hover 在那个 div 上,设置一个 activeIndex 到它的索引..

But as I said, this could be simply achieved by passive active className and then giving it z-index: 1但正如我所说,这可以简单地通过 passive active className 然后给它z-index: 1来实现

Here is the codesandbox这是代码框

This would be my first answer on stack, so I hope I meet the requirements.这将是我在堆栈上的第一个答案,所以我希望我能满足要求。 After trying many avenues it turns out the solution could be simpler with css. With the same setup as in question:在尝试了许多途径之后,事实证明使用 css 的解决方案可能更简单。使用与所讨论的相同的设置:

in your App.css在你的 App.css

.maplibregl-marker:hover {
  z-index: 1000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM