简体   繁体   English

放置CSS悬停菜单的正确方法是什么?

[英]What is the proper way to position a CSS hover menu?

I put together a fiddle with a very basic hover example. 我把一个小提琴与一个非常基本的悬停示例放在一起。

http://jsfiddle.net/qhj2P/ http://jsfiddle.net/qhj2P/

Here is the CSS: 这是CSS:

.button
{
    width: 50px;
    height: 15px;
    background-color: #090;
    padding: 5px;
}

.menu
{
    height: 200px;
    width: 50px;
    background-color: #900;
    display: none;    
}

I'm curious what the best way to position the menus is. 我很好奇放置菜单的最佳方法是什么。 How do I make it so that the first menu drops down right below the first button and the second menu drops down right below the second button? 如何使第一个菜单位于第一个按钮的正下方,而第二个菜单位于第二个按钮的正下方? I'm not so good at CSS positioning and the like. 我不太擅长CSS定位等。

Use a styled, unordered lists, not DIVs. 使用样式化的无序列表,而不要使用DIV。 Stick most of the styling on the A-tag with display:block . 使用display:block将大多数样式粘贴在A标签上。

<div class="menu">
<ul>
    <li><a href="...">Menu Item</a>
        <ul>
            <li><a href="...">Sub Item</a></li>
            <li><a href="...">Sub Item</a></li>
        </ul>
    </li>
    <li><a href="...">Menu Item</a></li>
</ul>
</div>

CSS: CSS:

.menu ul, .menu li {
    padding:0;
    margin:0;
    list-style-type:none
}

.menu li {
    display:inline-block;
    position:relative;
}

.menu a {
    font-family:arial;
    text-decoration:none;
    display:block;
    background-color:#c0c0c0;
    padding:3px;
    color:#000000
}

/* level two */

ul ul {
    position:absolute;
    display:none;    
}

ul ul a {
     background-color:#d0d0d0;
     margin-top:3px;   
}

The best way would be to use <ul> instead of your current setup. 最好的方法是使用<ul>而不是当前的设置。
http://jsfiddle.net/elclanrs/fHmvs/1/ http://jsfiddle.net/elclanrs/fHmvs/1/

<ul class="menu">
    <li>
        <a href="#">One</a>
        <ul>
            <li><a href="#">One</a></li>
            <li><a href="#">Two</a></li>
        </ul>
    </li>
    <li>
        <a href="#">Two</a>
        <ul>
            <li><a href="#">One</a></li>
            <li><a href="#">Two</a></li>
        </ul>
    </li>
</ul>

css: CSS:

.menu li {
    float: left;
    position: relative;
}
.menu ul {
    position: absolute;
    display: none;
    background: red;
}
.menu ul li { clear: both; }
.menu a {
    display: block;
    padding: .5em;
    margin: .1em;
}

jq: jq:

$('.menu li').hover(function() {
    $(this).children('ul').stop(1, 1).slideToggle();
});

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

相关问题 在内联 CSS 中编写 a:hover 的最佳方法是什么? - What is the best way to write a:hover in inline CSS? 检测Bootstrap下拉菜单选择的正确方法是什么? - What is the proper way to detect Bootstrap dropdown menu selections? 在AngularJS中突出显示当前活动菜单的正确方法是什么? - What is the proper way for highlighting currently active menu in AngularJS? 使悬停菜单适用于移动设备的最佳实践方法是什么? - What's the best practice way to make a hover menu work for mobile? 导航栏菜单位置和悬停 - Nav bar menu position and hover 当我将鼠标悬停在“产品”菜单上时,此代码需要进行哪些更改以使外观正确? - What changes are needed in this code to give a proper look when i hover on the Product menu? 使用ClientDependency Framework动态添加CSS或JS的正确方法是什么? - What is the proper way to dynamically add CSS or JS using ClientDependency Framework? 使用车把引用 css 和 js 文件的正确方法是什么? - What is the proper way of referencing css and js files with handlebars? 使用Jquery来停止css动画。 这样做的正确方法是什么? - Using Jquery to stop css animation. What is the proper way to do this? 将js,css,scss文件导入到webpack的正确方法是什么? - What is the proper way to import js, css, scss files to webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM