简体   繁体   English

CSS下拉菜单-鼠标移开后消失太快

[英]CSS Drop Down Menu - disappears too quickly on mouse out

I'm hoping one of you nice people can help. 我希望你们中的好人能为您提供帮助。 I have a pure CSS drop down menu, as part of the design it needs to be slightly away from the main menu item, the problem is that because of this design there's a gap - and so as soon as the mouse moves away from the sub menu it disappears. 我有一个纯CSS下拉菜单,作为设计的一部分,它需要稍微远离主菜单项,问题在于,由于这种设计存在间隙-因此,一旦鼠标移离子菜单,菜单它消失了。 I want it to hold their for a while longer so the user has more chance to click. 我希望它保持一段时间,以便用户有更多的点击机会。 Can anyone help? 有人可以帮忙吗?

I've set up a JS fiddle here . 我在这里设置了一个JS小提琴

<div id="menu" class="top">
<ul>
<li><a href="#">home</a></li>
<li><a href="#">about</a></li>
<li><a href="#">portfolio</a>
<ul>
<li><a href="#">Overview</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
</ul>
</li>
    </div>

#menu{
position:absolute;
left:0;
zoom:1;
background-color:#010;
}
#menu ul{
position:relative;
border-top: dotted 1px #fff;
border-bottom: dotted 1px #fff;
padding:2px 0 2px 0;
float:right;
zoom:1;
list-style:none;
}
#menu ul li{
margin:0;
font:16px/16px 'Open Sans', sans-serif;
padding:0 5px 0 0;
display:inline;
position:relative;
zoom:1;
}
#menu ul li a{
color:#fff;
text-decoration:none;
}
#menu ul li a:hover{
text-decoration:underline;
}
#menu ul li.selected a{
    color:#8dc63e;
}
#menu ul li ul {
  border: 2px solid #fff;
  border-radius:10px;
  background-color: #8dc63e;
  padding: 0;
  position: absolute;
  top: 45px;
  right: -30px;
  width: 150px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  display: none;
  opacity: 0;
  visibility: hidden;
  -webkit-transiton: opacity 0.8s;
  -moz-transition: opacity 0.8s;
  -ms-transition: opacity 0.8s;
  -o-transition: opacity 0.8s;
  -transition: opacity 0.8s;
}
#menu ul li ul li {   
  display: block;
  border-bottom: 1px solid #fff; 
  color: #fff;
  padding:10px;
}
#menu ul li ul li:hover { text-decoration:underline; }
#menu ul li.selected ul li a { color: #fff;}

#menu ul li:hover ul {
  display: block;
  opacity: 1;
  visibility: visible;
  z-index:250;
}
#menu ul li ul:after{
content: '';
position: absolute;
border-style: solid;
border-width: 0 15px 15px;
border-color: #fff transparent;
display: block;
width: 0;
z-index: 1;
top: -15px;
left: 64px; 
}

It seemed a bit of a mess, lots of duplication with display:none being set as well as opacity:0 and visibility:hidden. 似乎有点混乱,与display:none设置了很多重复,以及不透明性0和可见性:隐藏。 There are a few z-indexes in there as well. 那里也有一些z索引。 Not nice to maintain. 不好维护。

Really what you need is for the UL menu to be opacity 0, with a transition to fade it in over time, but not too quick, and then the hover state to be opacity 1. 真正需要的是UL菜单的不透明度为0,并随着时间的推移逐渐淡入,但又不是太快,然后将悬停状态变为不透明度1。

So from fiddling with your fiddle... 所以从摆弄你的小提琴...

#menu ul li ul {
  border: 2px solid #fff;
  border-radius:10px;
  background-color: #8dc63e;
  padding: 0;
  position: absolute;
  top: 45px;
  right: -30px;
  width: 150px;
  -webkit-box-shadow: none;
  -moz-box-shadow: none;
  box-shadow: none;
  opacity: 0;
  -webkit-transiton: opacity 8s;
  -moz-transition: opacity 8s;
  -ms-transition: opacity 8s;
  -o-transition: opacity 8s;
  -transition: opacity 8s;
}

and

#menu ul li:hover ul {
  display: block;
  opacity: 1;
  visibility: visible;
  z-index:250;
}

This one works, http://jsfiddle.net/ydF3B/1/ although I set the transition to be 8s from 0.8s... you might want to dig through and remove all the unneccessary styles and see what is actually going on. 尽管我将过渡设置为从0.8s转换为8s,但该方法还是有效的, http://jsfiddle.net/ydF3B/1 / ...您可能想要深入研究并删除所有不必要的样式,然后查看实际发生的情况。

Have fun :) 玩得开心 :)

EDIT Complete justification for my earlier comment about wanting to dig through it all, the fiddle above doesn't solve your issue as pointed out in the comments :) Instead a better approach is to make the area you are hovering over larger, so that when you pass over from the menu item onto the menu, there is no actual gap, even though there is a visual gap. 编辑我想对所有内容进行深入研究的早期评论的完整论证,上面的小提琴无法解决评论中指出的问题:)而是一种更好的方法是使您悬停的区域更大,以便当从菜单项转到菜单,即使有视觉间隙也没有实际间隙。 I did this by increasing the padding: http://jsfiddle.net/ydF3B/2/ 我通过增加填充来做到这一点: http : //jsfiddle.net/ydF3B/2/

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

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