Create a hover effect for labels
Hi, I am trying to create a hover effect for labels. My idea is that we can create two layers using the same label data and add them to the map. One layer will be the labels displayed on the map and the other one will be the hover effect layer. When hovering over a label, we can get the properties of that label and use properties like "id" or "name" to filter the hover effect layer. Here is what I have got so far: hover effect test
There are three problems:
- The hover effect does not always work.
- The respond of the hover effect is slow in low zoom levels. It is faster when you zoom in.
- The hover effect will be easily gone because I used "mouseenter" and "mouseleave" on the label layer. You may notice that if you move your mouse to the space between letters, the hover effect will be gone. Only when you move your mouse exactly on the letters, you will see the hover effect.
I wonder if there is a better way to create a hover effect for labels. Also, is there any way to create polygons like the collision boxes for the text labels.
Any help will be appreciated.
That's probably the best way for now. See #200 for a request for a built-in feature for this.
@jfirebaugh Thank you! The pseudo-class is already a built-in feature but not released to the public now? Where can I send a request for that built-in feature?
@Roxyi It's not (yet) a built-in feature -- issue #200 is the request to add it; that would be the place to watch for further discussion/progress.
Using a debounce function with layer filters can help a bit for your hover use case in the meantime @roti https://bl.ocks.org/ryanbaumann/0d72890cea4f97e0dbd10ea3cf7189b2
@anandthakker Thank you! Got it.
@ryanbaumann That's amazing. The debounce function helps improve the performance a lot. Thank you!