[ITA] Gli eventi onMouseEnter & onMouseLeave su tutti i browser

Reading Time: 1 minute

Utilizzare gli eventi onMouseEnter & onMouseLeave non solo su Internet Explorer non sarà più un problema! Basta una funzioncina javascript e prototype.

function extendMouseEvents(e) {
var elem = e.element();
var target = e.relatedTarget;
var p = stop = null;
if (!target || (elem !== target && !target.descendantOf(elem))) {
if (target && elem.descendantOf(target)) stop = target.childElements();
else if (target) {
p = elem.up();
while (!target.descendantOf(p)) p = p.up();
if (target.descendantOf(p)) stop = p.childElements();
}
if (stop) stop.invoke('observe', 'custom:mouseleave', function(e) { e.stop(); });
elem.fire('custom:mouseleave');
if (stop) stop.invoke('stopObserving', 'custom:mouseleave', function(e) { e.stop(); });
}
p = stop = null;
if (target && !elem.descendantOf(target)) {
if (!target.descendantOf(elem)) {
p = target.up();
while (!elem.descendantOf(p)) p = p.up();
stop = p.childElements();
} else
stop = elem.childElements();
if (stop) stop.invoke('observe', 'custom:mouseenter', function(e) { e.stop(); });
target.fire('custom:mouseenter');
if (stop) stop.invoke('stopObserving', 'custom:mouseenter', function(e) { e.stop(); });
}
}
Event.observe(document, 'mouseout', extendMouseEvents);

L’utilizzo è semplice:

Event.observe(element, 'custom:mouseenter', my_function_1);
// oppure
Event.observe(element, 'custom:mouseleave', my_function_2);

Leave a Reply

Your email address will not be published. Required fields are marked *