简体   繁体   English

将CSS悬停菜单转换为jQuery菜单

[英]Convert a CSS hover menu to jQuery Menu

I found a blog post on creating the menu found in gmail with css dropdown . 我发现了一篇博客文章,介绍如何使用css dropdown在gmail中找到菜单

The dropdown is displayed when we hover over the buttons but I want to convert this to a jQuery menu 当我们将鼠标悬停在按钮上时会显示下拉菜单,但我想将其转换为jQuery菜单

where the dropdown is displayed when we click on a button rather than on mouseover, Please help. 当我们单击按钮而不是鼠标悬停时显示下拉列表的位置,请提供帮助。

Thanks in advance . 提前致谢 。

http://jsfiddle.net/PSUkW/ http://jsfiddle.net/PSUkW/

It's really simple logic. 这是很简单的逻辑。 I replaced a few of the :hover CSS rules with a .clicked class and added this JS: 我用.clicked类替换了一些:hover CSS规则,并添加了以下JS:

$('.menu > ul > li').click(function() {
    $(this).siblings().removeClass('clicked');
    $(this).toggleClass('clicked');
});

Instead of allowing the browser to trigger the dropdown with the :hover pseudo class, I have jQuery toggle the class .clicked on each top-level li clicked. 我没有让浏览器使用:hover伪类触发下拉菜单,而是让jQuery在单击的每个顶级li上切换类.clicked This produces the same behavior, but with JS. 这会产生相同的行为,但使用JS。

This line: 这行:

$(this).siblings().removeClass('clicked');

Basically removes the clicked class from the siblings of that element, which allows the user to drop down only one menu at a time (otherwise overlap occurs). 基本上从该元素的同级中删除clicked类,这允许用户一次仅下拉一个菜单(否则会发生重叠)。

Demo: http://jsfiddle.net/PSUkW/5/ 演示: http//jsfiddle.net/PSUkW/5/

Pretty simply. 很简单。 Inside of the CSS adding styles and showing the dropdown on li:hover, you can update it to add a class: something like .menu ul li.click ul . 在CSS内添加样式并显示li:hover的下拉菜单时,您可以对其进行更新以添加一个类:诸如.menu ul li.click ul类的东西。 And then 2 lines of jQuery to target that click action. 然后使用2行jQuery来定位该点击动作。

See the code here: http://jsfiddle.net/PSUkW/3/ 在此处查看代码: http : //jsfiddle.net/PSUkW/3/

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

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