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

javascript - How to toggle popup in react-leaflet on mouse hover - Stack Overflow

programmeradmin1浏览0评论

I tried everything I found on google and I can't figure out how to trigger the popup.

<Marker
  position={this.props.position}
  onMouseOver={() => { openPopup() }}
  onMouseOut={() => { closePopup() }}
>
  <Popup>
     "HI"
  </Popup>
</Marker>

Note: I know I can't trigger the function openPopup there its just to show where I want to implement the trigger function to toggle the popup on mouse hover.

Can someone please help, Thanks.

I tried everything I found on google and I can't figure out how to trigger the popup.

<Marker
  position={this.props.position}
  onMouseOver={() => { openPopup() }}
  onMouseOut={() => { closePopup() }}
>
  <Popup>
     "HI"
  </Popup>
</Marker>

Note: I know I can't trigger the function openPopup there its just to show where I want to implement the trigger function to toggle the popup on mouse hover.

Can someone please help, Thanks.

Share Improve this question edited Aug 2, 2018 at 22:01 Mustkeem K 8,7782 gold badges34 silver badges44 bronze badges asked Aug 2, 2018 at 21:03 Belal El-MonajjedBelal El-Monajjed 911 gold badge1 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

Leaflet Marker object is accessible via event.target property of mouseover and mouseout events, so popup could be opened/closed like this:

<Marker
    position={position}
    onMouseOver={(e) => {
      e.target.openPopup();
    }}
    onMouseOut={(e) => {
      e.target.closePopup();
    }}
  >
    <Popup>Sydney</Popup>
  </Marker>

Demo

Using React Leaflet v3, the solution is easier and cleaner, use Tooltip instead of Popup, e.g.:

<Marker position={this.props.position}>
  <Tooltip>I appear on mouse over</Tooltip>
</Marker>

In particular, I have found useful to also add sticky property for Tooltip. There are more examples for tooltips in the documentation, which covers basic tooltips, sticky tooltips or permanent tooltips, with offsets and many more.

发布评论

评论列表(0)

  1. 暂无评论