CSS如何使下拉项目显示在顶部

时间:2014-05-22 13:28:56

标签: html css

我在互联网上找到了一些我非常喜欢的代码。它使用CSS创建一个很酷的下拉菜单。虽然它有效,但问题是当我将多个这些控件放在彼此之下时。如果下面有多个下拉菜单,实际的下拉列表将不会出现在下面的其他下拉菜单之上。这是我的代码:

    <div id="dd" class="wrapper-dropdown-3">
        <span id="spTransport">Transport</span>
        <ul class="dropdown">
            <li><a href="#"><i class="icon-envelope icon-large"></i>Classic mail</a></li>
            <li><a href="#"><i class="icon-truck icon-large"></i>UPS Delivery</a></li>
            <li><a href="#"><i class="icon-plane icon-large"></i>Private jet</a></li>
        </ul>
    </div>
    <div id="dd2" class="wrapper-dropdown-3">
        <span id="spTransport2">Transport</span>
        <ul class="dropdown">
            <li><a href="#"><i class="icon-envelope icon-large"></i>Classic mail</a></li>
            <li><a href="#"><i class="icon-truck icon-large"></i>UPS Delivery</a></li>
            <li><a href="#"><i class="icon-plane icon-large"></i>Private jet</a></li>
        </ul>
    </div>
    <div id="dd3" class="wrapper-dropdown-3">
        <span id="spTransport3">Transport</span>
        <ul class="dropdown">
            <li><a href="#"><i class="icon-envelope icon-large"></i>Classic mail</a></li>
            <li><a href="#"><i class="icon-truck icon-large"></i>UPS Delivery</a></li>
            <li><a href="#"><i class="icon-plane icon-large"></i>Private jet</a></li>
        </ul>
    </div>

这是CSS:

::selection {
    background: transparent; 
}

::-moz-selection {
    background: transparent; 
}
/*
.wrapper-demo {
    margin: 60px 0 0 0;
    *zoom: 1;
    font-weight: 400;
}

.wrapper-demo:after {
    clear: both;
    content: "";
    display: table;
}
*/
/* DEMO 3 */

.wrapper-dropdown-3 {
    /* Size and position */
    position: relative;
    width: 300px;
    margin: 0 auto;
    /*padding: 10px;*/
    padding: 3px;

    /* Styles */
    /*background: #fff;*/
    background: #3E3E47;
    border-radius: 7px;
    border: 1px solid rgba(0,0,0,0.15);
    box-shadow: 0 1px 1px rgba(50,50,50,0.1);
    cursor: pointer;
    outline: none;

    /* Font settings */
    font-weight: bold;
    /*color: #8AA8BD;*/
    color: white;

    opacity:.99;
}

.wrapper-dropdown-3:after {
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    right: 15px;
    top: 50%;
    margin-top: -3px;
    border-width: 6px 6px 0 6px;
    border-style: solid;
    /*border-color: #8aa8bd transparent;*/
    border-color: white transparent;

    opacity:.99;
}

.wrapper-dropdown-3 .dropdown {
  /* Size & position */
    position: absolute;
    top: 140%;
    left: 0;
    right: 0;

    /* Styles */
    background: black;
    border-radius: inherit;
    border: 1px solid rgba(0,0,0,0.17);
    box-shadow: 0 0 5px rgba(0,0,0,0.1);
    font-weight: normal;
    -webkit-transition: all 0.5s ease-in;
    -moz-transition: all 0.5s ease-in;
    -ms-transition: all 0.5s ease-in;
    -o-transition: all 0.5s ease-in;
    transition: all 0.5s ease-in;
    list-style: none;

    /* Hiding */
    opacity: 0;
    pointer-events: none;



    padding: 0;
    margin: 0;
}

.wrapper-dropdown-3 .dropdown:after {
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    bottom: 100%;
    right: 15px;
    border-width: 0 6px 6px 6px;
    border-style: solid;
    border-color: #000 transparent;    



    padding: 0;
    margin: 0;

}

.wrapper-dropdown-3 .dropdown:before {
    content: "";
    width: 0;
    height: 0;
    position: absolute;
    bottom: 100%;
    right: 13px;
    border-width: 0 8px 8px 8px;
    border-style: solid;
    border-color: rgba(0,0,0,0.1) transparent;    




    padding: 0;
    margin: 0;
}

.wrapper-dropdown-3 .dropdown li a {
    display: block;
    /*padding: 10px;*/
    padding: 4px;
    text-decoration: none;
    /*color: #8aa8bd;*/
    color: white;
    border-bottom: 1px solid #e6e8ea;
    box-shadow: inset 0 1px 0 rgba(255,255,255,1);
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    transition: all 0.3s ease-out;
}

.wrapper-dropdown-3 .dropdown li i {
    float: right;
    color: inherit;
}

.wrapper-dropdown-3 .dropdown li:first-of-type a {
    border-radius: 7px 7px 0 0;
}

.wrapper-dropdown-3 .dropdown li:last-of-type a {
    border: none;
    border-radius: 0 0 7px 7px;
}

/* Hover state */

.wrapper-dropdown-3 .dropdown li:hover a {
    /*background: #f3f8f8;*/
    /*background: #3A3A49;*/
    background: #3E3E47;
}

/* Active state */

.wrapper-dropdown-3.active .dropdown {
    opacity: 1;
    pointer-events: auto;
}

/* No CSS3 support */

.no-opacity       .wrapper-dropdown-3 .dropdown,
.no-pointerevents .wrapper-dropdown-3 .dropdown {
    display: none;
    opacity: 1; /* If opacity support but no pointer-events support */
    pointer-events: auto; /* If pointer-events support but no pointer-events support */
}

.no-opacity       .wrapper-dropdown-3.active .dropdown,
.no-pointerevents .wrapper-dropdown-3.active .dropdown {
    display: block;
}

如何让下拉项目显示在单击下方的主要下拉控件的上方?因此,如果我点击第一个下拉列表,那么&#34; li&#34;项目会显示,但它们会显示在单击下方的下拉列表下方。

演示:http://jsfiddle.net/aamir/36evU/

提前致谢,

布拉德

1 个答案:

答案 0 :(得分:0)

有一个选项下拉菜单横向真希望我的例子会有所帮助 的 HTML

<body>
    <ul class="menu">
        <li><a href="">main</a></li>
        <li><a href="">company</a>
            <ul>
                <li><a href="">works</a></li>
                <li><a href="">clients</a></li>
                <li><a href="">partners</a></li>
                <li><a href="">menu</a></li>
                <li><a href="">social</a></li>
            </ul>
        </li>
        <li><a href="">menu1</a></li>
        <li><a href="">menu2</a></li>
        <li><a href="">menu3</a></li>
        <li><a href="">menu4</a></li>
        <li><a href="">menu5</a></li>
    </ul>
</body>

<强> CSS

*{
    margin: 0;
    padding: 0;
}

body {
    margin: 20px;
}
/*Горизонтальное выпадающее меню*/
ul.menu {
    margin: 0;
    padding: 0;
    display: inline-block;
    background-color: #2b2b2b;
    background-repeat: no-repeat;
    background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#4f4f4f), to(#2b2b2b));
    background-image: -webkit-linear-gradient(#4f4f4f, #2b2b2b);
    background-image: -moz-linear-gradient(top, #4f4f4f, #2b2b2b);
    background-image: -ms-linear-gradient(#4f4f4f, #2b2b2b);
    background-image: -o-linear-gradient(#4f4f4f, #2b2b2b);
    background-image: linear-gradient(#4f4f4f, #2b2b2b);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#4f4f4f', endColorstr='#2b2b2b', GradientType=0); 
    border-radius: 4px;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    width: 900px;
}

ul.menu li {
    float: left;
    list-style: none;
    border-right: 1px solid #4f4f4f;
    position: relative;
}

ul.menu li a {
    color: white;
    display: block;
    font-family: Arial;
    font-size: 13px;
    padding: 8px 20px 10px;
    text-decoration: none;
    border-right: 1px solid #2b2b2b;
}

ul.menu li a:hover {
    background-color: #2b2b2b;
}

ul.menu li:first-child a:hover {
    border-radius: 4px 0 0 4px;
    -webkit-border-radius: 4px 0 0 4px;
    -moz-border-radius: 4px 0 0 4px;
}
/*Выпадающее меню*/
ul.menu li ul {
    display: none;
}

ul.menu li:hover ul {
    display: block;
    position: absolute;
    top: 34px;
    border-radius: 0 0 4px 4px;
    -webkit-border-radius: 0 0 4px 4px;
    -moz-border-radius: 0 0 4px 4px;
    background-color: #2b2b2b;
    z-index: 1000;
}

ul.menu li:hover ul li {
    float: none;
    width: 150px;
    border-right: none;
}

ul.menu li:hover ul li a {
    border-top: 1px solid #4f4f4f;
    border-right: none;
    padding: 6px 20px 8px;
}

ul.menu li:hover ul li a:hover {
    background-color: transparent;
    color: #c1deff;
}

ul.menu li:hover ul li:first-child a:hover {
    border-radius: 0;
    -webkit-border-radius: 0;
    -moz-border-radius: 0;
}   

demo