Onmouseover儿童div问题和事件冒泡 [英] Onmouseover child div problem and event bubbling

查看:104
本文介绍了Onmouseover儿童div问题和事件冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小div(悬停)一个大的。

我将onmouseover和onmouseout事件分配给包装器div。

用于图像标题翻转动画。

I have a small div above (hover) a big one.
I assign onmouseover and onmouseout events to the wrapper div.
For image caption roll-over animation.

问题是当鼠标位于标题本身之上时,会导致不需要的结果(可能是事件冒泡)。

The problem is when the mouse is above the caption itself, causing an unwanted result(probably event bubbling).

另一个问题是:有时当你将鼠标从外部移动到容器时,你会得到一个三重序列:(它应该只是2):

- 我超过了b $ b -I-out-
-我结束了 -

And another problem: sometimes when you move mouse from outside to container you get a a triple sequence: (it should be just 2):
-I am over- -I am out- -I am over-

如何让它发挥作用? (没有jquery

必须适用于所有浏览器。

How to make it work? (no jquery)
must work on all browsers.

演示

我添加了firebug控制台日志,以便更好地进行调试。

I have added firebug console log, to a better debugging.

更新:

我在RollOverDescription中添加了这个(不在在线演示中):

UPDATE:
I've added this (not in the online demo) in RollOverDescription:

if (!eventHandle) var eventHandle = window.event;               
var srcEle = eventHandle.srcElement.id;             
if(srcEle=="imageDescription" ){
    return;
}

但它没有帮助。

推荐答案

这篇文章关于quirksmode(靠近底部)有一个解释你正在经历的东西和一个可能对你有帮助的脚本。有很多关于鼠标事件的跨浏览器信息

This article on quirksmode ( near the bottom ) has an explanation of what you are experiencing and a script that might help you. There is a lot of cross browser info regarding mouse events too

好的,这里有一些工作代码。我不保证这是最有效的,或者它不会导致IE中的内存泄漏(或者它在IE中工作 - 请告诉我)。这就是为什么人们使用库,更安全,更容易。

OK, here's some working code. I don't promise this is the most efficient or that it won't cause memory leaks in IE (or that it works in IE - please let me know ). This is why people use libraries, much safer and easier.

// a general purpose, cross browser event adder
// returns a function that if run removes the event
function addEvent( el, eventType, handler, capturing ) {
    if( el.addEventListener ) {
        el.addEventListener( eventType, handler, capturing || false );
        var removeEvent = function() { el.removeEventListener( eventType, handler, capturing || false ) };
    } else if( el.attachEvent ) {
        var fn = function() {
            handler.call( el, normalise( window.event ) );
        };
        el.attachEvent( 'on'+eventType, fn );
        var removeEvent = function(){ el.detachEvent( 'on'+eventType, fn ) };
    }
    function normalise( e ) {
        e.target = e.srcElement;
        e.relatedTarget = e.toElement;

        e.preventDefault = function(){ e.returnValue = false };
        e.stopPropagation = function(){ e.cancelBubble = true };
        return e;
    };
    return removeEvent;
};



// adds mouseover and mouseout event handlers to a dom element
// mouseover and out events on child elements are ignored by this element
// returns a function that when run removes the events
// you need to send in both handlers - an empty function will do
function addMouseOverOutEvents( element, overHandler, outHandler ) {

    function out( e ) {
        var fromEl = e.target;
        var toEl = e.relatedTarget;
        // if the mouseout didn't originate at our element we can ignore it
        if( fromEl != element ) return;
        // if the element we rolled onto is a child of our element we can ignore it
        while( toEl  ) {
            toEl = toEl.parentNode;
            if( toEl == element ) return;
        }
        outHandler.call( element, e );
    }

    function over( e ) {
        var toEl = e.target;
        var fromEl = e.relatedTarget;
        // if the mouseover didn't originate at our element we can ignore it
        if( toEl != element ) return;
        // if the element we rolled from is a child of our element we can ignore it
        while( fromEl  ) {
            fromEl = fromEl.parentNode;
            if( fromEl == element ) return;
        }
        overHandler.call( element, e );
    }

    var killers = [];
    killers.push( addEvent( element, 'mouseover', over ) );
    killers.push( addEvent( element, 'mouseout', out ) );
    return function() {
        killers[0]();
        killers[1]();
    }
}

使用示例:

// add the events
var remover = addMouseOverOutEvents(
    document.getElementById( 'elementId' ),
    function( e ) {
        this.style.background = 'red';
        console.log( 'rolled in: '+e.target.id );
    },
    function( e ) {
        this.style.background = 'blue'
        console.log( 'rolled out: '+e.target.id );
    }    
);

//remove the events
remover();

这篇关于Onmouseover儿童div问题和事件冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆