鼠标悬停时的Div扩展和收缩?

时间:2014-10-02 06:05:14

标签: javascript jquery html onmouseover onmouseout

我正在制作一个带有jquery动画效果的下拉菜单,

var mexpand = false;
function toggleMenu() {
    if (!mexpand) {
        $('jQuery selector').css({ "background": "url('Images/bnbgmenu.jpg') repeat-x top left" });
        $("#NavDiv").animate({ height: "200px" });
        mexpand = true;
    }
    else {
        $("#NavDiv").animate({ height: "35px" });
        $('jQuery selector').css({ "background": "url('Images/bnbguser.jpg') repeat-x top left" });
        mexpand = false;
    }
}

请查看此jsfiddle

我想将鼠标悬停在菜单图标上展开,我想在鼠标离开下拉菜单时收缩。

但是我遇到了鼠标悬停和鼠标输出问题,你可以在小提琴中看到。

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用jQuery设置功能,而不是调用该函数。 将您的图标图像设为类icon,然后使用以下jQuery。

$( ".icon" )
    .mouseenter(function() {
        $("#NavDiv").animate({ height: "200px" });
    }
);
$("#NavDiv")  
    .mouseleave(function() {
        $(this).animate({ height: "35px" });
    }
);

请参阅http://jsfiddle.net/hp4jh9f7/4/

答案 1 :(得分:0)

您应该分别使用onmouseenteronmouseleave个事件代替onmouseoveronmouseout

请参阅此fiddler http://jsfiddle.net/hp4jh9f7/2/