• <tfoot id='kg5Ik'></tfoot>
  • <i id='kg5Ik'><tr id='kg5Ik'><dt id='kg5Ik'><q id='kg5Ik'><span id='kg5Ik'><b id='kg5Ik'><form id='kg5Ik'><ins id='kg5Ik'></ins><ul id='kg5Ik'></ul><sub id='kg5Ik'></sub></form><legend id='kg5Ik'></legend><bdo id='kg5Ik'><pre id='kg5Ik'><center id='kg5Ik'></center></pre></bdo></b><th id='kg5Ik'></th></span></q></dt></tr></i><div id='kg5Ik'><tfoot id='kg5Ik'></tfoot><dl id='kg5Ik'><fieldset id='kg5Ik'></fieldset></dl></div>

    <legend id='kg5Ik'><style id='kg5Ik'><dir id='kg5Ik'><q id='kg5Ik'></q></dir></style></legend>

    <small id='kg5Ik'></small><noframes id='kg5Ik'>

      <bdo id='kg5Ik'></bdo><ul id='kg5Ik'></ul>

      1. 即使元素已经消失,悬停状态也会在转换期间保持

        时间:2023-11-29

          • <legend id='riqou'><style id='riqou'><dir id='riqou'><q id='riqou'></q></dir></style></legend>

            <small id='riqou'></small><noframes id='riqou'>

              <tbody id='riqou'></tbody>

              <i id='riqou'><tr id='riqou'><dt id='riqou'><q id='riqou'><span id='riqou'><b id='riqou'><form id='riqou'><ins id='riqou'></ins><ul id='riqou'></ul><sub id='riqou'></sub></form><legend id='riqou'></legend><bdo id='riqou'><pre id='riqou'><center id='riqou'></center></pre></bdo></b><th id='riqou'></th></span></q></dt></tr></i><div id='riqou'><tfoot id='riqou'></tfoot><dl id='riqou'><fieldset id='riqou'></fieldset></dl></div>
            1. <tfoot id='riqou'></tfoot>
              • <bdo id='riqou'></bdo><ul id='riqou'></ul>
                • 本文介绍了即使元素已经消失,悬停状态也会在转换期间保持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  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;
                  }
                  

                  JSFiddle

                  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:

                  1. Is this behaviour normal (compliant with the norms)?
                  2. 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?

                  解决方案

                  Part 1 of your question:

                  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.

                  So here are my questions:

                  1. Is this behaviour normal (compliant with the norms)?

                  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 lower div 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.

                  1. You touch the edge and mouseover and mouseenter are fired in quick succession (hover).
                  2. As a result the inner div translates.
                  3. Do nothing. No event is fired and so nothing happens.
                  4. Move the mouse inside the outer div. mousemove fires and the inner div is still translated.
                  5. Slowly move the mouse out. mouseout and mouseleave are fired in quick succession and the inner div 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:

                  An event is the representation of some occurrence (such as a mouse click on the presentation of an element, the removal of child node from an element, or any number of other possibilities) which is associated with its event target. Each event is an instantiation of one specific event type.

                  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:

                  After an event completes all the phases of its propagation path, its Event.currentTarget must be set to null and the Event.eventPhase must be set to 0 (NONE). All other attributes of the Event (or interface derived from Event) are unchanged (including the Event.target attribute, which must continue to reference the event target).

                  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. On mouseenter the div 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 this div) and now the user-agent finds that the mouse pointer is no longer over the element and immediately mouseout and mouseleave events fire (after firing mousemove of course) causing the div to translate back.

                  Part 2 of your question:

                  2.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?

                  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 the div 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 on mouseleave. The same element can be transform-ed when you click. If you simply click without moving the mouse, the tooltip hides.

                  Here, if you click, the element is being translated and then no further hover 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 after click. You still handle events on the element itself. No need for timeout as it is handled by the css itself. If you mouseout, the tooltip will hide after a delay.

                  Hope that helps.

                  这篇关于即使元素已经消失,悬停状态也会在转换期间保持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇::hover 拖放到元素上 下一篇:在悬停一个伪元素时,让另一个伪元素出现?

                  相关文章

                  <small id='CMdkM'></small><noframes id='CMdkM'>

                    <legend id='CMdkM'><style id='CMdkM'><dir id='CMdkM'><q id='CMdkM'></q></dir></style></legend>

                      <i id='CMdkM'><tr id='CMdkM'><dt id='CMdkM'><q id='CMdkM'><span id='CMdkM'><b id='CMdkM'><form id='CMdkM'><ins id='CMdkM'></ins><ul id='CMdkM'></ul><sub id='CMdkM'></sub></form><legend id='CMdkM'></legend><bdo id='CMdkM'><pre id='CMdkM'><center id='CMdkM'></center></pre></bdo></b><th id='CMdkM'></th></span></q></dt></tr></i><div id='CMdkM'><tfoot id='CMdkM'></tfoot><dl id='CMdkM'><fieldset id='CMdkM'></fieldset></dl></div>

                      • <bdo id='CMdkM'></bdo><ul id='CMdkM'></ul>

                      <tfoot id='CMdkM'></tfoot>