Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何知道两个元素上的mouseLeave_Javascript_Jquery_Html_Css_Mouseevent - Fatal编程技术网

Javascript 如何知道两个元素上的mouseLeave

Javascript 如何知道两个元素上的mouseLeave,javascript,jquery,html,css,mouseevent,Javascript,Jquery,Html,Css,Mouseevent,我试图在悬停时创建一个下拉菜单,如下所示 在网站的标题下 我使用jQuery,我使用事件mouseEnter和mouse leave。 使用以下代码 var LastThis = null; $(".divOpener, #floatingNewNav") .mouseleave(function(event) { console.log

我试图在悬停时创建一个下拉菜单,如下所示

在网站的标题下

我使用jQuery,我使用事件mouseEnter和mouse leave。 使用以下代码

 var LastThis = null;
                $(".divOpener, #floatingNewNav")
                .mouseleave(function(event)
                {
                    console.log(event);
                    if($(event.toElement).attr("id") != "floatingNewNav") // do not close since we leaved the element but we got on the floating nav.
                    {
                        $("#floatingNewNav").hide(0);

                        if(LastThis.attr("id") == "ShopByBrand")
                        {
                            LastThis.removeClass("NavSelected");
                            $("#"+LastThis.attr("id")+"_Nav").css("display","none");
                        }
                        if(LastThis.attr("id") == "ShopByCategory")
                        {
                            LastThis.removeClass("NavSelected");
                            $("#"+LastThis.attr("id")+"_Nav").css("display","none");
                        }
                        if(LastThis.attr("id") == "ShopByPrice")
                        {
                            LastThis.removeClass("NavSelected");
                            $("#"+LastThis.attr("id")+"_Nav").css("display","none");
                        }
                    }
                });
                $(".divOpener")
                .mouseenter(function()
                {
                    LastThis = $(this);
                    if($(this).attr("id") == "ShopByBrand")
                    {
                        $("#"+$(this).attr("id")+"_Nav").css("display","block");
                        $(this).addClass('NavSelected');
                    }
                    if($(this).attr("id") == "ShopByCategory")
                    {
                        $("#"+$(this).attr("id")+"_Nav").css("display","block");
                        $(this).addClass('NavSelected');
                    }
                    if($(this).attr("id") == "ShopByPrice")
                    {
                        $("#"+$(this).attr("id")+"_Nav").css("display","block");
                        $(this).addClass('NavSelected');
                    }
                    var DivPosition = $(this).parent().position();
                    var Position = $(this).position();

                    var curTop = DivPosition.top;
                    var curLeft = Position.left;
                    var curWidth = $(this).width();
                    var curHeight = $(this).parent().height();
                    var DivWidth = $(this).parent().width();

                    var WidthOfNav = 400;

                    var OffSetLeft = (curLeft+(curWidth/2)-(WidthOfNav/2)+WidthOfNav)-(DivPosition.left+DivWidth);
                    var OffSetLeft = (OffSetLeft>0?OffSetLeft:0);

                    $("#floatingNewNav").css("position","absolute");
                    $("#floatingNewNav").css("height","100px");
                    $("#floatingNewNav").css("top",(curTop+curHeight)+"px");
                    $("#floatingNewNav").css("left",((curLeft+(curWidth/2))-(WidthOfNav/2))-OffSetLeft+"px");
                    $("#floatingNewNav").css("width",WidthOfNav+"px");

                    $("#floatingNewNav").show(0);
                });
Html

.divOpener类是

在该示例中,您将看到它在chrome浏览器(即safari)中有效,但在firefox上无效。因为某些原因,firefox不会在
事件中返回对象
toElement

我的问题 有没有一种方法可以创建一个包含两个元素的事件mouseEnter和mouseLeave,并假装它只是一个元素,这样我就不需要了

if($(event.toElement).attr(“id”)!=“floatingNewNav”)


谢谢你的帮助,也很抱歉让人困惑的问题。

要创建一个出现在hover上的下拉菜单,就像你例子中的那个,我会使用CSS。见下例:

HTML


要创建一个出现在hover上的下拉菜单,如您的示例中所示,我将使用CSS。见下例:

HTML


要创建一个出现在hover上的下拉菜单,如您的示例中所示,我将使用CSS。见下例:

HTML


要创建一个出现在hover上的下拉菜单,如您的示例中所示,我将使用CSS。见下例:

HTML


为了抛开一个不同的概念,您的图片会尖叫“jQuery选项卡”:-,所以我使用了简单的选项卡,并为每个选项卡创建了一个jQuery触发器事件

JS


不是很优雅,但我会再玩一些,看看它是否可以“优雅化”。

为了抛开一个不同的概念,你的图片尖叫着“jQuery选项卡”:-,所以我使用了简单的选项卡,并为每个选项卡创建了一个jQuery触发事件

JS


不是很优雅,但我会再玩一些,看看它是否可以“优雅化”。

为了抛开一个不同的概念,你的图片尖叫着“jQuery选项卡”:-,所以我使用了简单的选项卡,并为每个选项卡创建了一个jQuery触发事件

JS


不是很优雅,但我会再玩一些,看看它是否可以“优雅化”。

为了抛开一个不同的概念,你的图片尖叫着“jQuery选项卡”:-,所以我使用了简单的选项卡,并为每个选项卡创建了一个jQuery触发事件

JS

不优雅,但我会再玩一些,看看它是否可以“优雅化”。

你可以简单地做到这一点

你可以做很多事情来重构这段代码。事实上,小提琴里还有一些额外的东西,我现在要把它们拿出来,我会很快更新的

更新:

这是最后的代码

var LastThis = null;
var openers = $(".divOpener, #floatingNewNav");

openers.mouseleave(function (event) {
    $("#floatingNewNav").hide();
    LastThis.removeClass("NavSelected");
    $("#" + LastThis.attr("id") + "_Nav").hide();
});

openers.mouseenter(function () {
    if (!$(this).is('#floatingNewNav')) {
        LastThis = $(this);
    }
    $("#" + LastThis.attr("id") + "_Nav").show();
    LastThis.addClass('NavSelected');

    var DivPosition = LastThis.parent().position();
    var Position = LastThis.position();

    var curTop = DivPosition.top;
    var curLeft = Position.left;
    var curWidth = LastThis.width();
    var curHeight = LastThis.parent().height();
    var DivWidth = LastThis.parent().width();

    var WidthOfNav = 400;

    var OffSetLeft = (curLeft + (curWidth / 2) - (WidthOfNav / 2) + WidthOfNav) - (DivPosition.left + DivWidth);
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0);

    $("#floatingNewNav").css({
        'position': 'absolute',
        'height': '100px',
        'top': (curTop + curHeight) + 'px',
        'left': ((curLeft + (curWidth / 2)) - (WidthOfNav / 2)) - OffSetLeft + 'px',
        'width': WidthOfNav + 'px'
    }).show();
});

作为一项建议,只有在正确构建html的情况下,这些菜单才能在css中完成。查看凯文的结构回答

你可以简单地做到这一点

你可以做很多事情来重构这段代码。事实上,小提琴里还有一些额外的东西,我现在要把它们拿出来,我会很快更新的

更新:

这是最后的代码

var LastThis = null;
var openers = $(".divOpener, #floatingNewNav");

openers.mouseleave(function (event) {
    $("#floatingNewNav").hide();
    LastThis.removeClass("NavSelected");
    $("#" + LastThis.attr("id") + "_Nav").hide();
});

openers.mouseenter(function () {
    if (!$(this).is('#floatingNewNav')) {
        LastThis = $(this);
    }
    $("#" + LastThis.attr("id") + "_Nav").show();
    LastThis.addClass('NavSelected');

    var DivPosition = LastThis.parent().position();
    var Position = LastThis.position();

    var curTop = DivPosition.top;
    var curLeft = Position.left;
    var curWidth = LastThis.width();
    var curHeight = LastThis.parent().height();
    var DivWidth = LastThis.parent().width();

    var WidthOfNav = 400;

    var OffSetLeft = (curLeft + (curWidth / 2) - (WidthOfNav / 2) + WidthOfNav) - (DivPosition.left + DivWidth);
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0);

    $("#floatingNewNav").css({
        'position': 'absolute',
        'height': '100px',
        'top': (curTop + curHeight) + 'px',
        'left': ((curLeft + (curWidth / 2)) - (WidthOfNav / 2)) - OffSetLeft + 'px',
        'width': WidthOfNav + 'px'
    }).show();
});

作为一项建议,只有在正确构建html的情况下,这些菜单才能在css中完成。查看凯文的结构回答

你可以简单地做到这一点

你可以做很多事情来重构这段代码。事实上,小提琴里还有一些额外的东西,我现在要把它们拿出来,我会很快更新的

更新:

这是最后的代码

var LastThis = null;
var openers = $(".divOpener, #floatingNewNav");

openers.mouseleave(function (event) {
    $("#floatingNewNav").hide();
    LastThis.removeClass("NavSelected");
    $("#" + LastThis.attr("id") + "_Nav").hide();
});

openers.mouseenter(function () {
    if (!$(this).is('#floatingNewNav')) {
        LastThis = $(this);
    }
    $("#" + LastThis.attr("id") + "_Nav").show();
    LastThis.addClass('NavSelected');

    var DivPosition = LastThis.parent().position();
    var Position = LastThis.position();

    var curTop = DivPosition.top;
    var curLeft = Position.left;
    var curWidth = LastThis.width();
    var curHeight = LastThis.parent().height();
    var DivWidth = LastThis.parent().width();

    var WidthOfNav = 400;

    var OffSetLeft = (curLeft + (curWidth / 2) - (WidthOfNav / 2) + WidthOfNav) - (DivPosition.left + DivWidth);
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0);

    $("#floatingNewNav").css({
        'position': 'absolute',
        'height': '100px',
        'top': (curTop + curHeight) + 'px',
        'left': ((curLeft + (curWidth / 2)) - (WidthOfNav / 2)) - OffSetLeft + 'px',
        'width': WidthOfNav + 'px'
    }).show();
});

作为一项建议,只有在正确构建html的情况下,这些菜单才能在css中完成。查看凯文的结构回答

你可以简单地做到这一点

你可以做很多事情来重构这段代码。事实上,小提琴里还有一些额外的东西,我现在要把它们拿出来,我会很快更新的

更新:

这是最后的代码

var LastThis = null;
var openers = $(".divOpener, #floatingNewNav");

openers.mouseleave(function (event) {
    $("#floatingNewNav").hide();
    LastThis.removeClass("NavSelected");
    $("#" + LastThis.attr("id") + "_Nav").hide();
});

openers.mouseenter(function () {
    if (!$(this).is('#floatingNewNav')) {
        LastThis = $(this);
    }
    $("#" + LastThis.attr("id") + "_Nav").show();
    LastThis.addClass('NavSelected');

    var DivPosition = LastThis.parent().position();
    var Position = LastThis.position();

    var curTop = DivPosition.top;
    var curLeft = Position.left;
    var curWidth = LastThis.width();
    var curHeight = LastThis.parent().height();
    var DivWidth = LastThis.parent().width();

    var WidthOfNav = 400;

    var OffSetLeft = (curLeft + (curWidth / 2) - (WidthOfNav / 2) + WidthOfNav) - (DivPosition.left + DivWidth);
    var OffSetLeft = (OffSetLeft > 0 ? OffSetLeft : 0);

    $("#floatingNewNav").css({
        'position': 'absolute',
        'height': '100px',
        'top': (curTop + curHeight) + 'px',
        'left': ((curLeft + (curWidth / 2)) - (WidthOfNav / 2)) - OffSetLeft + 'px',
        'width': WidthOfNav + 'px'
    }).show();
});


作为一项建议,只有在正确构建html的情况下,这些菜单才能在css中完成。查看凯文的结构回答

看起来你把事情复杂化了

跟踪鼠标的方向如何?如果鼠标向下,请不要隐藏下面的菜单项

检查这把小提琴。。

检查了它在Mozilla和chrome等中的工作情况

 var mY = 0;
$('div.menuItems').mousemove(function(e){
// moving upward
if (e.pageY < mY) {
    flag = "upward";
} else {
    flag = "downward";
}
// set new mY after doing test above
mY = e.pageY;
});
var-mY=0;
$('div.menuItems').mousemove(函数(e){
//向上移动
如果(e.pageY
谢谢,
jf_它

看起来你把事情复杂化了

跟踪鼠标的方向如何?如果鼠标向下,请不要隐藏下面的菜单项

检查这把小提琴。。

检查了它在Mozilla和chrome等中的工作情况

 var mY = 0;
$('div.menuItems').mousemove(function(e){
// moving upward
if (e.pageY < mY) {
    flag = "upward";
} else {
    flag = "downward";
}
// set new mY after doing test above
mY = e.pageY;
});
var-mY=0;
$('div.menuItems').mousemove(函数(e){
//向上移动
如果(e.pageY
谢谢,
jf_它

看起来你把事情复杂化了

跟踪鼠标的方向如何?如果鼠标向下,请不要隐藏下面的菜单项

检查这把小提琴。。

检查了它在Mozilla和chrome等中的工作情况

 var mY = 0;
$('div.menuItems').mousemove(function(e){
// moving upward
if (e.pageY < mY) {
    flag = "upward";
} else {
    flag = "downward";
}
// set new mY after doing test above
mY = e.pageY;
});
var-mY=0;
$('div.menuItems').mousemove(函数(e){
//向上移动
如果(e.pageY
谢谢,
jf_它

看起来你把事情复杂化了

跟踪鼠标的方向如何?如果鼠标向下,请不要隐藏下面的菜单项

检查这把小提琴。。

检查了它在Mozilla和chrome等中的工作情况

 var mY = 0;
$('div.menuItems').mousemove(function(e){
// moving upward
if (e.pageY < mY) {
    flag = "upward";
} else {
    flag = "downward";
}
// set new mY after doing test above
mY = e.pageY;
});
var-mY=0;
$('div.menuItems').mousemove(函数(e){
//向上移动
如果(e.pageY
谢谢, jf_it

你的if似乎是