我有一个小 div 位于(悬停)一个大 div 之上。
我将 onmouseover
和 onmouseout
事件分配给包装器 div。
用于图像标题翻转动画。
问题是当鼠标位于标题本身之上时,会导致不想要的结果(可能是事件冒泡)。
另一个问题:有时当您将鼠标从外部移动到容器时,您会得到一个三重序列:(它应该只是 2):
-我已经结束了-
-我出去了-
-我已经结束了-
如何让它发挥作用? (无 jQuery)
必须适用于所有浏览器。
我添加了firebug控制台日志,以更好地调试。
更新:
我已在 RollOverDescription
中添加了此内容(不在在线演示中):
if (!eventHandle) var eventHandle = window.event;
var srcEle = eventHandle.srcElement.id;
if(srcEle=="imageDescription" ){
return;
}
但这并没有帮助。
最佳答案
This article在 quirksmode(靠近底部)上有对您所经历的情况的解释以及可能对您有帮助的脚本。还有很多关于鼠标事件的跨浏览器信息
好的,这是一些工作代码。我不保证这是最有效的或者它不会导致 IE 中的内存泄漏(或者它在 IE 中有效 - 请让我知道)。这就是人们使用图书馆的原因,更安全、更容易。
// 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();
关于javascript - Onmouseover child div 问题和事件冒泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2206828/