简体   繁体   English

将鼠标悬停在两个元素上可将效果设置为一个元素

[英]hover over two elements set effect to one element

I have this list item in my nav , It has a dropDown menu 我的导航栏中有此列表项,它具有下拉菜单

 <li class="projects">
      <a href="#">Projects</a>
      <i class="fa fa-angle-down" aria-hidden="true"></i>
      <ul class="list-unstyled myDropDown">
             <li><a href="#">King SALEH Bridge</a></li>
             <li><a href="#">Internationl future schools</a></li>
             <li><a href="#">Elwakeel Companies Group</a></li>
      </ul>
 </li>

and here is the styles 这是样式

nav .myDropDown{
    display:none;
    position:absolute;
    min-height:160px;
    background-color:#ffb700;
    top:calc(15px + 100%)
}

and I want to display .myDropDown when hovering on li.projects and it still displayed when hovering on .myDropDown itself , and it disappear when the mouse became not in li.projects or .myDropDown . 我想将鼠标悬停在li.projects上时显示.myDropDown,并且仍然将鼠标悬停在.myDropDown本身上时显示,并且当鼠标不在li.projects或.myDropDown中时它消失。

I have this jquery code , but it doesn't work ?! 我有这个jquery代码,但是不行吗?

 $("nav .projects").hover(function(){
     $("nav .myDropDown").css("display","block");
 });
 $("nav .myDropDown").hover(function(){
           $(this).css("display","block");
  },function(){
           $(this).css("display","none");
 });

Why not only CSS? 为什么不仅是CSS?

 ul.myDropDown{ display: none; } li.projects:hover > ul.myDropDown{ display: block; } 
 <li class="projects"> <a href="#">Projects</a> <i class="fa fa-angle-down" aria-hidden="true"></i> <ul class="list-unstyled myDropDown"> <li><a href="#">King SALEH Bridge</a></li> <li><a href="#">Internationl future schools</a></li> <li><a href="#">Elwakeel Companies Group</a></li> </ul> </li> 

EDIT: I changed your top value and it works: 编辑:我更改了您的最高价值,并且它起作用:

 li.projects { position: relative; } ul.myDropDown{ display:none; position:absolute; min-height:160px; background-color:#ffb700; top: 0px; } li.projects:hover > ul.myDropDown{ display: block; } 
 <li class="projects"> <a href="#">Projects</a> <i class="fa fa-angle-down" aria-hidden="true"></i> <ul class="list-unstyled myDropDown"> <li><a href="#">King SALEH Bridge</a></li> <li><a href="#">Internationl future schools</a></li> <li><a href="#">Elwakeel Companies Group</a></li> </ul> </li> 

You should probably use the CSS answers provided. 您可能应该使用提供的CSS答案。 But if you want to use javascript you should do it like this: 但是,如果您想使用javascript,则应这样做:

 $("nav .projects").hover(function(){
    $(this).find(".myDropDown").css("display","block");
 }, function(){
     $(this).find(".myDropDown").css("display","none");
 });

You can't keep the top:calc(15px + 100%) if you want it to overlap. 如果要重叠,则不能保留top:calc(15px + 100%) If you keep a gap between the dropdown and the link you're removing it before entering it. 如果您在下拉菜单和链接之间留有空隙,请在输入之前删除它。 It's the same with the CSS solution. CSS解决方案也是如此。

https://jsfiddle.net/80tbzcjh/1/ https://jsfiddle.net/80tbzcjh/1/

用这种方式

li:hover .myDropDown{dispaly:block;}

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

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