Consider a simple element, and its associated CSS:
<div id="content">Hover me !</div>
#content {
width: 100px;
height: 100px;
}
#content:hover {
transform: translateY(500px);
transition: transform 1s 500ms;
}
The principle is straightforward: while the element is hovered, it must go down. The problem is, when the mouse doesn't move, that the :hover
state is maintained even if the element is not physically below the mouse anymore (due to the translation). The state seems to be updated only after an mouse move.
Notice the cursor (a pointer) and its relative position with the element!
That's a real problem when a JavaScript function must be executed only if the mouse is on an element, after a timeout
:
// The mouseleave event will not be called during the transition,
// unless the mouse move !
element.on('mouseenter', executeAfterTimeout);
element.on('mouseleave', cancelTimeout);
So here are my questions:
- Is this behaviour normal (compliant with the norms)?
- What are the solutions to avoid this problem?
Edit : To give you a context, here is what I want to do concretely: with JavaScript, I display a tooltip when the mouse is on an element (and hide it when the mouse leaves it). But the same element can be transform
-ed when the user click on it. If the user simply clicks without moving the mouse, the tooltip will remain displayed, which is a real problem. How can I detect that the element is gone?
It's a solution to use JavaScript and a class to indicate the status. In your case, you could use
mouseover
event to toggle a class like this:CSS
jsFiddle
The other solution is to use a wrapper as hover block
CSS
jsFiddle
Notice, this two solutions have different behaviors for different requirements.
Part 1 of your question:
Yes. This behaviour is normal. Although not specified verbatim in the standards, it is mentioned in detail here: http://www.w3.org/TR/2013/WD-DOM-Level-3-Events-20131105
Take this fiddle as reference: http://jsfiddle.net/Blackhole/h7tb9/3/
The upper
div
has mouse-events bound to it directly. The lowerdiv
has mouse-event bound to its parent. Pick up the lower one. Move the mouse slowly at one edge and watch the console to see what happens.mouseover
andmouseenter
are fired in quick succession (hover).div
translates.div
.mousemove
fires and the innerdiv
is still translated.mouseout
andmouseleave
are fired in quick succession and the innerdiv
translates back to its original position.This is described here: http://www.w3.org/TR/2013/WD-DOM-Level-3-Events-20131105/#events-mouseevents under the section Mouse Event Order.
Step 3 above is important. Because you are doing nothing, no event is fired and hence nothing happens. If the inner
div
were to bounce back to its original position in this step, then it would mean that an activation happened without any event!This is in line with the definition of event as the document in this section: http://www.w3.org/TR/2013/WD-DOM-Level-3-Events-20131105/#glossary-event says:
Now have a look at the document here: http://www.w3.org/TR/2013/WD-DOM-Level-3-Events-20131105/#event-flow, just before the section 3.2 starts, it says:
The last line (in parentheses) is important. The event.target continues to reference the event target even after the event completes.
Now pick the upper
div
in the fiddle for reference. Onmouseenter
thediv
itself is translated. It does not matter if it moves away from below the mouse pointer. The event.target is still referencing to it and if no other mouse event occurs, nothing happens and it remains translated. The moment you move your mouse (anywhere in or out), the activation occurs on the event.target (which is still thisdiv
) and now the user-agent finds that the mouse pointer is no longer over the element and immediatelymouseout
andmouseleave
events fire (after firingmousemove
of course) causing thediv
to translate back.Part 2 of your question:
If you look at the implementation in the lower
div
in this fiddle: http://jsfiddle.net/abhitalks/h7tb9/2/ ; as compared to the upper div, there is no flutter/jitter when mousing over. This is because rather than thediv
itself, the events are being handled on the parent.So, that could be one solution for your use case.
See this demo: http://jsfiddle.net/Blackhole/nR8t9/9/
This addresses your edit. Tooltip gets displayed on
mouseover
. Tooltip gets hidden onmouseleave
. The same element can be transform-ed when youclick
. If you simplyclick
without moving the mouse, the tooltip hides.Here, if you
click
, the element is being translated and then no furtherhover
action would happen. The tooltip itself is implemented using a:before
pseudo-element. This separates out the tooltip and the element which you want to change afterclick
. You still handle events on the element itself. No need for timeout as it is handled by the css itself. If youmouseout
, the tooltip will hide after a delay.Hope that helps.
Check if this is the behaviour you want to accomplish. Some are example styles, adjust as you please.
http://jsfiddle.net/U44Zf/9/
I've added this javascript snippet to control the click state
This behavior is normal to prevent the element from bouncing under the cursor. Imagine the transition would revert as soon as the element is away from the cursor. As soon as the cursor has left the element, it would go back, so the cursor is again above the element and it moves down. This way it would bounce up and down at the edge of the cursor.
One solution would be to implement the transition with JavaScript instead of CSS, then the element will "bounce". But is this really the desired behavior? What exactly are you trying to do?
My suggestion is to look at this problem another way: if an element is going to be transitioned when you
click
on it. Why not just execute your callback onclick
instead ofmouseleave
?I am assuming the tooltip has some connection to the element you
mouseenter
, in which casemouseleave
andclick
are effectively the same - they both cause mouse pointer to not be over the element anymore (regardless of how browser behaves).PS: note that in your example, how
mouseenter
andmouseleave
fire also depends on whether you set thetransition
as default property or as a:hover
state property, since this looks like an area where browser vendors are free to optimize as they please, you should probably avoid they in the first place.transition on #content:hover
transition on #content
This behavior is normal and can not be changed. It is correctly implemented according to the specification @Stasik linked to.
If you have to change this behavior, you could use javascript with jquery instead of css pseudo classes. I created a jsfiddle to demonstrate a possible approach using the
.ismouseover()
jQuery extension by @Ivan Castellanos provided here.