在悬停其子元素时保持父元素的CSS样式

时间:2016-09-08 16:04:30

标签: javascript jquery html hover jquery-hover

我创建了一个带有菜单的导航栏,只要您将鼠标悬停在显示蓝线的部分中。我的问题是,在一个部分中有一个子菜单,我希望当你将鼠标悬停在子菜单的部分(子)时,父级仍会显示蓝线,因为它仍然悬停。

//Display Submenu
$('nav .submenu').hover(function() {
    $(this).children('ul').animate(
        {'height':'toggle'}, 600, 'swing'
    );
});

//Blue Line animation
$('nav ul > li a').hover(function(){
    $(this).next('.blueLine').animate(
        {'width':'100%'}, 200, 'linear')
},function(){
    $(this).next('.blueLine').animate(
        {'width':'0'}, 200, 'linear');      
});

Here's a working fiddle

1 个答案:

答案 0 :(得分:1)

如果你保持HTML结构,你可以试试这个:

$('nav ul > li a').hover(function(){
  $(this).next('.blueLine').stop().animate(
    {'width':'100%'}, 200, 'linear');
  $(this).closest('ul').prev().stop().css('width','100%');
},function(){
  $(this).next('.blueLine').stop().animate(
    {'width':'0'}, 200, 'linear');
  $(this).closest('ul').prev().stop().animate(
    {'width':'0'}, 200, 'linear');
});

JSFiddle