What is the difference between jQuery's mouseout() and mouseleave()?

前端 未结 4 1076
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 05:11

What is the difference between jQuery\'s mouseout() and mouseleave()?

4条回答
  •  再見小時候
    2020-11-28 06:06

    There can be times when mouseout is a better choice than mouseleave.

    For example, let's say you've created a tooltip that you want displayed next to an element on mouseenter. You use setTimeout to prevent the tooltip from popping up instantly. You clear the timeout on mouseleave using clearTimeout so if the mouse leaves the tooltip won't be displayed. This will work 99% of the time.

    But now let's say the element you have a tooltip attached to is a button with a click event, and let's also assume this button prompts the user with either a confirm or alert box. The user clicks the button and the alert fires. The user pressed it fast enough that your tooltip didn't have a chance to pop up (so far so good).

    The user presses the alert box OK button, and the mouse leaves the element. But since the browser page is now in a locked state, no javascript will fire until the OK button has been pressed, meaning your mouseleave event WILL NOT FIRE. After the user presses OK the tooltip will popup (which is not what you wanted).

    Using mouseout in this case would be the appropriate solution because it will fire.

提交回复
热议问题