简体   繁体   English

jQuery下拉菜单悬停状态

[英]jQuery dropdown menu hover state

My problem is: 我的问题是:

I have a drop down menu and i want that when I hover the menu the text color change and when I hover the submenu the hover state stays for both. 我有一个下拉菜单,我希望当我将菜单悬停在菜单上时,文本颜色会发生变化,而当我将鼠标悬停在子菜单上时,两者的悬停状态都将保持不变。 I use this code: 我使用以下代码:

$("ul li").hover(function () {

    $(this).stop().animate({ backgroundColor: "white"}, 500);

},
function () {

    $(this).stop().animate({ backgroundColor: "black"}, 400);

});

}

to animate the background-color on hover in menu and submenu. 在菜单和子菜单中将鼠标悬停时的背景色设置为动画。

I want to change the color of the text on hover to (diferent for menu and submenu, not the same color animation). 我想将悬停时的文本颜色更改为(菜单和子菜单不同,颜色动画不同)。 For this I use this code:(Submenu example, for menu example, change the selector to $('ul.menu li a') 为此,我使用以下代码:(子菜单示例,例如菜单示例,将选择器更改为$('ul.menu li a')

$('ul.submenu li a').hover(function () {

$(this).css({color:'#FFFFFF'});

},
function () {

$(this).css({color:'#00FF00'});

});

All This works fine, but when I hover the submenu the menu returns to the original color state (because the mouseleave is activated on menu hover out). 所有这一切都很好,但是当我将鼠标悬停在子菜单上时,菜单返回到原始颜色状态(因为在菜单上将鼠标悬停激活时,鼠标悬停了)。

All I want is that when I hover submenu the hover state in menu stays active as well. 我想要的是,当我悬停子菜单时,菜单中的悬停状态也保持活动状态。

I've tried many things but all give me problems, only thing that works is css, but I need to control the text colors dinamically too. 我已经尝试了很多方法,但是都给我带来了问题,只有可行的方法是css,但是我也需要动态地控制文本颜色。

CSS That Works: 有效的CSS:

ul li:hover a {
    color: #FFF;
}

(with this css code I control the menu color with the css and when I hovered the submenu the menu stays in active state, but the submenu works with jquery .hover). (通过此CSS代码,我可以使用CSS控制菜单颜色,并且当我将鼠标悬停在子菜单上时,菜单保持处于活动状态,但是该子菜单与jquery .hover一起使用)。

Can anyone Help? 谁能帮忙? Thanks! 谢谢!

HTML Menu: HTML菜单:

<ul class="menu">

      <li><a href="#">text</a></li>

      <li><a href="#">text</a>

        <ul class="submenu">
          <li><a href="#">text</a></li>
          <li><a href="#">text</a></li>
          <li><a href="#">text</a></li>
        </ul>

      </li>

      <li><a href="#">text</a>

</ul>

If you want hover to set the current color until you hover some other li, use classes. 如果要悬停以设置当前颜色,直到悬停其他li,请使用类。

css: CSS:

ul.submenu li a { color: #0f0; }
ul.submenu li a.hovered { color: #fff; }

js: js:

$('ul.submenu li a').mouseover( function() {
   if (!$(this).hasClass('hovered')) {
      $('ul.submenu li a.hovered').removeClass('hovered');
      $(this).setClass('hovered');
   }
});

I hope I understood you correctly. 我希望我能正确理解你。 Good luck with your project. 祝您项目顺利。

[edit] [编辑]

Oh hm, maybe you want it so when you go off to the submenu, your parent li shouldn't lose its color, despite you mousing out. 哦,嗯,​​也许您想要它,所以当您进入子菜单时,即使您将鼠标移出该菜单,您的父本li也不应失去其颜色。 If so, the same idea is applicable, you'd just want that mouseover code to select on the menu item mouseover (so mousing over the menu unsets all other menus and sets this one), then you just also need to have it remove your menu's class color if you mouse out of its submenu's ul. 如果是这样,同样的想法是适用的,您只希望在菜单项mouseover上选择该mouseover代码(因此将鼠标悬停在菜单上会取消设置所有其他菜单,并设置该菜单),那么您还需要使其删除您的菜单。菜单的班级颜色(如果您将鼠标从其子菜单ul中移出)。 I don't know what your menus look like structure-wise, so I can't comment on a working CSS for this selector. 我不知道您的菜单在结构上看起来是什么样的,因此我无法对此选择器使用有效的CSS进行评论。

Oh and finally you'd want to have a css rule vs. submenu hover to handle your submenu's highlighting. 哦,最后,您想要一个css规则与子菜单悬停来处理子菜单的突出显示。

Sorry for the rambling answer... 对不起,这个答案很混乱...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM