Mouseover/mouseenter not fired by browser on animated/moving element

我的未来我决定 提交于 2019-12-23 21:51:16

问题


If you have an element that has an animation to move it around, the mouseover and mouseenter events aren't fired unless the mouse is moved by the user. To demonstrate try the block of code below with jQuery.

If you put your mouse in front of the moving div so you're not moving the mouse when the div passes by then the mouseover isn't fired and the block doesn't stop.

In Firefox the mouseover event is fired without moving the mouse manually over the div, but only if you've moved the mouse at least once.

My question is there a workaround so an element moved under the mouse cursor will still have its mouseover event fired?

<script>
$(function() {
    var move = 10,
        left = 0,
        width = 100;

    var stop = setInterval(function() {
        left += move;
        $('#mydiv').css('left', left);
        if (left < 0 || (left + width > parseInt($(window).width()))) 
            move = -1 * move;
    }, 10);

    $('#mydiv').mouseover(function() { clearInterval(stop); });
});
</script>
<div id="mydiv" style="width: 100px; height: 100px; position: absolute; top: 0; left: 0; background-color: Black;">&#160</div>

I know the example is contrived, but it is just to demonstrate the issue. Thanks for any help!


回答1:


This is a browser bug.

The only workaround would be to track the global mouse coordinates in a document-level mousemove handler, then check during the animation whether the element covers those coordinates.



来源:https://stackoverflow.com/questions/3963803/mouseover-mouseenter-not-fired-by-browser-on-animated-moving-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!