【问题标题】:'mouseenter' event recording that mouse entered from inside the element'mouseenter' 事件记录鼠标从元素内部进入
【发布时间】:2013-05-28 14:12:53
【问题描述】:

请看this fiddle

忽略 CSS,它不重要。

这个jQuery 代码的目的是告诉与鼠标进入.container 的方向相反的方向。我知道它看起来很丑陋,但请暂时忽略它。方向为n(北)、ne(东北)等...

现在,如果您像疯子一样打开console 并在div.container 周围移动鼠标,最终您会在console 中看到undefined。 (div.container 是不可见的,并且围绕着圆形按钮)

undefined只有当getMovementDirection()中的x === 0 && y === 0才可能,这意味着输入div.containermousediv.button(球形按钮)内,这是不可能。

那么,我的问题是,这段代码发生了什么?

-感谢您的帮助

PS: 标题需要改进。

jQuery code

(function($) {
    var $container = $('div.container'),
        $button = $('div.button');

    $container.on('mouseenter', function(e) {
        var mouseLocation = {
            x: e.pageX,
            y: e.pageY
        }
        var buttonLocation = {
            x: $button.offset().left,
            y: $button.offset().top
        }

        var loc = getMovementDirection (mouseLocation, buttonLocation, jQuery);
        console.log(loc);
    });
})(jQuery);

function getMovementDirection (mouse, container, $) {
    var width = $('div.button').outerWidth(),
        height = $('div.button').outerHeight();
    var x, y;

    if (mouse.x < container.x) { x = -1; }
    else if (mouse.x < container.x + width) { x = 0; }
    else { x = 1; }

    if (mouse.y < container.y)  { y = -1; }
    else if (mouse.y < container.y + width) { y = 0; }
    else { y = 1; }

         if ( x === -1 && y === -1 ) { return 'se'; }
    else if ( x ===  0 && y === -1 ) { return 's';  }
    else if ( x ===  1 && y === -1 ) { return 'sw'; }
    else if ( x === -1 && y ===  0 ) { return 'e';  }
    // x === 0 && y === 0 is forbidden
    else if ( x ===  1 && y ===  0 ) { return 'w';  }
    else if ( x === -1 && y ===  1 ) { return 'ne'; }
    else if ( x ===  0 && y ===  1 ) { return 'n';  }
    else if ( x ===  1 && y ===  1 ) { return 'nw'; }
}

【问题讨论】:

  • 这与问题无关,但您正在检查 mouse.y 与 container.y + 宽度而不是 container.y + 高度。我唯一能想到的是从触发事件到捕获鼠标位置数据时鼠标所在位置的轻微延迟。如果有人在您的页面上癫痫发作并在这种情况下使用按钮做其他事情,也许会想出一个案例? :\
  • 实际上,我正计划制作那个小游戏,你尝试点击一个按钮,它就会一直逃跑。我知道 jQuery 不是最适合它,但它只是为了好玩。但是这个时间延迟的事情,如果是真的,毁了我所有的计划 :( PS:在这种情况下,容器宽度 = 高度,但感谢纠正我,我忘了改变它

标签: jquery html css


【解决方案1】:

这可能不是最优雅的解决方案,甚至不是修复方法,但我用另一种获取鼠标数据的方式更新了小提琴:

http://jsfiddle.net/e3XNm/3/

它不使用jquery内置的mouseenter事件,而是使用js原生的mouseover事件,当mouseover是按钮时它会忽略。我认为 jquery 的执行方式可能会产生一些额外的开销(我根本没有查看它的代码),所以为什么不将其精简为更基本的东西。另外,我从这里偷了 addevent 代码:http://ejohn.org/projects/flexible-javascript-events/

addEvent( $container[0], 'mouseover', function (e) {
    if (e.target === $button[0])
        return;

    // get event details here

    var loc = getMovementDirection(mouseLocation, buttonLocation, jQuery);

    // debugging
    if (loc != undefined) {
        var width = $('div.button').outerWidth(),
            height = $('div.button').outerHeight();
        console.log(mouseLocation.x, mouseLocation.y, buttonLocation.x, buttonLocation.y, width, height);
        console.log(loc);
    } else {
        console.log("wut");            
    }

我根本无法让“wut”被解雇,但也许我只是不够紧张

更新

这是在每次鼠标悬停时运行以执行 mouseenter 行为的 jquery 代码

// Create mouseenter/leave events using mouseover/out and event-time checks
jQuery.each({
    mouseenter: "mouseover",
    mouseleave: "mouseout"
}, function( orig, fix ) {
    jQuery.event.special[ orig ] = {
        delegateType: fix,
        bindType: fix,

        handle: function( event ) {
            var ret,
                target = this,
                related = event.relatedTarget,
                handleObj = event.handleObj;

            // For mousenter/leave call the handler if related is outside the target.
            // NB: No relatedTarget if the mouse left/entered the browser window
            if ( !related || (related !== target && !jQuery.contains( target, related )) ) {
                event.type = handleObj.origType;
                ret = handleObj.handler.apply( this, arguments );
                event.type = fix;
            }
            return ret;
        }
    };
});

如果在不同的元素上重新运行一些处理代码,可能会出现一些延迟。

【讨论】:

  • Wut 没有被解雇,因为你添加了这个:if (e.target === $button[0]) return;。如果是 wut 应该触发的情况,则发起返回调用。当我删除它时,我能够得到wut 解雇
  • 参见thisthisthis。似乎没有人有解决方案。
  • 我添加了 if 语句,因为您不关心鼠标是否在按钮上,您只关心它进入容器时的位置。 mouseover 当它越过父元素中包含的任何元素时触发。这段代码所做的只是在触发事件的元素是容器时检查位置。
  • 哦,好的,我现在明白了。由坏。当鼠标从按钮移动到容器时,有什么方法可以忽略这种情况?
  • 检查 fromElement 的事件,如果是按钮,则返回 - if (e.target === $button[0] || e.fromElement === $button[0]) return;
猜你喜欢
  • 2012-04-22
  • 1970-01-01
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 1970-01-01
相关资源
最近更新 更多