I want to implement a floating information pop-up (or how to call it), that appears when the cursor is over an element and stays within a fixed distance to the cursor. For instance something more less like here:
http://www.nytimes.com/interactive/2008/09/15/business/20080916-treemap-graphic.html
Say, I have 100 divs for which I want the pop-up to appear. I thought about doing it in two ways:
-
For each div, in HTML code I have a hidden pop-up div, on
mousemoveI show it, move to the cursor’s position and onmouseleaveI hide it. Each div has itsmousemove(x100) andmouseenterevent bound (x100). -
I have a single
mousemovefor the main area of the page,for each div onmouseenterandmouseleaveI show / hide a pop-up. There is a singlemousemove(x1) andmouseenter+mouseleavefor each div (x100).
My questions are:
-
Is This The Way? Or there is some better jQuery mechanism to use?
-
Is there any difference in performance when I have a single
mousemoveand 100mousemovecallbacks?
these are called tooltips (there are many javascript/jQuery plugins for these)
in regards to rolling your own, you seem to have it figured out, you’ll need to create
mouseover/mousemove/mouseoutevents for the divs. With jQuery this would look something like:example jsfiddle
As for performance: jsperf – mousemove parent vs children
setting the
mousemoveevent on the container does indicate a faster operations/sec in the above test case but in real world use it’s unlikely you’d notice a difference.here’s an example where only the container’s
mousemoveevent is setexample jsfiddle #2