Ruby's Louvre

The Crankiness of Belief achieves Great , not the Trick of Regulation.

导航

公告

获取relatedTarget属性

在做mouseenter与mouseleave的兼容时,我们需要用到事件对象的relatedTarget属性

function getRelatedTarget(e) {
    var t = e.relatedTarget
    if (t) {
        return t
    }
    return e.fromElement === e.target ?
        e.toElement :
        e.fromElement;
}
String("mouseenter,mouseleave").replace(/\w+/g, function (type) {
    eventHooks[type] = function (dom) {
        var eventType = type === "mouseenter" ? "mouseover" : "mouseout";
        addEvent(dom, eventType, function (e) {
            var t = getRelatedTarget(e)
            if (!t || (t !== dom && !dom.contains(t))) {
                dispatchEvent(e, type, true);
            }
        });
    };
});

posted on 2017-08-17 21:04 司徒正美 阅读(...) 评论(...) 编辑 收藏