[英]make dropdown menu still visible even when mouse hover is lost
I need to have main menu with dropdown menu items. 我需要具有带下拉菜单项的主菜单。 So, when user hovers menu item then dropdown menu is shown. 因此,当用户将鼠标悬停在菜单项上时,将显示下拉菜单。 This is already done by CSS and :hover selector. 这已经由CSS和:hover选择器完成了。 However, usually when :hover is lost (mouse is moved outside menu item element) then appropriate dropodown also disappears. 但是,通常当:hover丢失(鼠标移到菜单项元素外部)时,适当的下拉菜单也会消失。 But what I need - menu is still seen and disappears only on click. 但是我需要的是-菜单仍然可见,并且仅在单击时消失。
One example of this effect is here: http://theemon.com/f/forever/Livepreview/agency-wedding/index.html 这种效果的一个示例在这里: http : //theemon.com/f/forever/Livepreview/agency-wedding/index.html
However I do not understand which CSS or JS creates this effect, so I cannot add snipper here (if I could, I would not ask). 但是我不知道哪个CSS或JS会产生这种效果,因此我无法在此处添加窥探器(如果可以的话,我不会问)。 In this particular example, when you hover menu item "Pages", dropdown menu appears, but it doesn't disappear when :hover is lost. 在此特定示例中,当您将鼠标悬停在菜单项“页面”上时,将显示下拉菜单,但是当:hover丢失时,下拉菜单不会消失。 It stays there. 它留在那里。 I am not able to find, what makes this effect - is it some JS or CSS? 我找不到,是什么原因造成的-是JS还是CSS?
HTML: HTML:
<ul class="navigation clearfix">
<li class="active">
<a href="#">HOME</a>
</li>
<li>
<a href="#">pages</a>
<ul class="drop-down">
<li>
<a href="event.html">Event</a>
</li>
<li>
<a href="blog.html">Blog</a>
</li>
<li>
<a href="blog-detail-page.html">Blog-Detail</a>
</li>
<li>
<a href="travel-info.html">Travel-Info</a>
</li>
<li>
<a href="404-page.html">404</a>
</li>
</ul>
</li>
</ul>
Some CSS: 一些CSS:
.clearfix {
display: block;
}
ul {
list-style-type: none;
}
ul, ol {
font-size: 100%;
line-height: 1.5;
list-style: none;
}
.navigation > li {
padding: 16px 0px 36px 26px;
float: left;
position: relative;
line-height: 1.5em;
}
.navigation > li:hover .drop-down {
top: 76px;
left: auto;
left: 16px;
right: 0;
}
.drop-down {
position: absolute;
top: 100px;
width: 160px;
z-index: 999;
left: -9999px;
opacity: 0;
transition: top .2s ease, opacity .2s ease;
}
You can add a class like .open
on mouseenter
to your li
element which contains a dropdown. 您可以在.open
类的类mouseenter
到包含下拉列表的li
元素中。
Then do this: 然后执行以下操作:
var dropdown = $(".is-dropdown");
dropdown
.on("mouseenter", function() {
$(this)
.addClass("open");
})
.on("click", function() {
$(this).removeClass("open");
});
CODE SNIPPET: 代码片段:
var dropdown = $(".is-dropdown"); dropdown .on("mouseenter", function() { $(this) .addClass("open"); }) .on("click", function() { $(this).removeClass("open"); });
.clearfix { display: block; } ul { list-style-type: none; } ul, ol { font-size: 100%; line-height: 1.5; list-style: none; } .navigation > li { padding: 16px 0px 36px 26px; float: left; position: relative; line-height: 1.5em; } .drop-down { position: absolute; top: 100px; width: 160px; z-index: 999; left: -9999px; opacity: 0; transition: top .2s ease, opacity .2s ease; } .navigation > .open .drop-down { top: 76px; left: auto; left: 16px; right: 0; opacity: 1; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="navigation clearfix"> <li class="active"> <a href="#">HOME</a> </li> <li class="is-dropdown"> <a href="#">pages</a> <ul class="drop-down"> <li> <a href="event.html">Event</a> </li> <li> <a href="blog.html">Blog</a> </li> <li> <a href="blog-detail-page.html">Blog-Detail</a> </li> <li> <a href="travel-info.html">Travel-Info</a> </li> <li> <a href="404-page.html">404</a> </li> </ul> </li> </ul>
I am sure there are other parameters to this functionality, but here is a base level solution for you. 我确定此功能还有其他参数,但这是为您提供的基本解决方案。
$(document).ready(function() { $('.drop-down').slideUp(0); //hides all your drop downs $('.navigation li:has(> ul.drop-down)').on('mouseenter', function() { $(this).children('ul').slideDown(); }); $('.closeEm').on('click', function(e) { e.preventDefault(); // this stops the page jumpping on click $('ul.drop-down').slideUp(); }); });
.clearfix { display: block; } ul { list-style-type: none; } ul, ol { font-size: 100%; line-height: 1.5; list-style: none; } .navigation > li { padding: 16px 0px 36px 26px; float: left; position: relative; line-height: 1.5em; } .navigation li { background-color: #ddd; } .drop-down { position: absolute; top: 70px; width: 160px; z-index: 999; background-color: #eee; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="navigation clearfix"> <li class="active"> <a href="#">HOME</a> </li> <li> <a href="#">pages</a> <ul class="drop-down"> <li> <a href="event.html">Event</a> </li> <li> <a href="blog.html">Blog</a> </li> <li> <a href="blog-detail-page.html">Blog-Detail</a> </li> <li> <a href="travel-info.html">Travel-Info</a> </li> <li> <a href="404-page.html">404</a> </li> </ul> </li> <li> <a href="#">pages</a> </li> <li> <a href="#">pages</a> </li> <li> <a href="#">pages</a> <li> <a href="#" class="closeEm">Close Dropdowns</a> </li> </ul>
我不确定我是否了解您要尝试执行的操作,但是我想此示例将是您所需要的: http : //www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_hover
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.