为我的页面制作可滚动的下拉菜单

时间:2014-12-12 09:46:49

标签: html css drop-down-menu

我想为我的网站制作一个可滚动的下拉菜单。我阅读了很多关于可滚动下拉菜单的页面,但他们的代码不起作用。但是,我的下拉菜单仍然无法显示可滚动的下拉菜单。

HTML

<li>
            <a href="#">Team Profile</a>

            <ul>
              <li><a href="#">Chelsea</a></li>
        <li><a href="#">Man City</a></li>
        <li><a href="#">Southampton</a></li> 
        <li><a href="#">Man Utd</a></li>
        <li><a href="#">Arsenal</a></li>
        <li><a href="#">Liverpool</a></li>
        <li><a href="#">West Ham</a></li>
        <li><a href="#">Newcastle</a></li>
        <li><a href="#">Leichester</a></li>
        <li><a href="#">QPR</a></li>
        <li><a href="#">Burnley</a></li>
        <li><a href="#">Aston Villa</a></li>
        <li><a href="#">Stoke City</a></li>
        <li><a href="#">Crystal Palace</a></li>
        <li><a href="#">Hull City</a></li>
        <li><a href="#">Sunderland</a></li>
        <li><a href="#">Tottenham Hotspur</a></li>
        <li><a href="#">Everton</a></li>
        <li><a href="#">Swansea</a></li>
        <li><a href="#">West Brom</a></li>
            </ul>

        </li>

CSS

.myDropDown
{
   height: 50px;
   overflow: auto;
}
.nav ul {
    *zoom:1;
    list-style:none;
    margin:0;
    padding:0;
    background:#333;
}
.nav ul:before,.nav ul:after {
    content:"";
    display:table;
}
.nav ul:after {
    clear:both;
}
.nav ul > li {
    float:left;
    position:relative;
}
.nav a {
    display:block;
    padding:10px 20px;
    line-height:1.2em;
    color:#fff;
    border-left:1px solid #595959;
}
.nav a:hover {
    text-decoration:none;
    background:#595959;
}
.nav li ul {
    background:#273754;
}
.nav li ul li {
    width:200px;
}
.nav li ul a {
    border:none;
}
.nav li ul a:hover {
    background:rgba(0,0,0,0.2);
}
.nav li ul {
    position:absolute;
    left:0;
    top:36px;
    z-index:1;
    max-height:0;
    overflow:hidden;
    -webkit-transform:perspective(400) rotate3d(1,0,0,-90deg);
    -webkit-transform-origin:50% 0;
    -webkit-transition:350ms;
    -moz-transition:350ms;
    -o-transition:350ms;
    transition:350ms;
}
.nav ul > li:hover ul {
    max-height:1000px;
    -webkit-transform:perspective(400) rotate3d(0,0,0,0);
}
*                                   { margin: 0; padding: 0; }
body                                { font: 15px Helvetica, Sans-Serif; } 
html                                { overflow-y: scroll; }
#page-wrap                          { width: 720px; margin: 25px auto; } 
p                                   { margin: 0 0 8px 0; }
a                                   { text-decoration: none; }
img                                 { vertical-align: middle; }
a img                               { border: 0; 180}
ul                                  { list-style: none; }
h1                                  { margin: 0 0 10px 0; }

/* 
    LEVEL ONE
*/
ul.dropdown                         { position: relative; width: 100%; }
ul.dropdown li                      { font-weight: bold; float: left; width: 180px; background: #ccc; position: relative; }
ul.dropdown a:hover                 { color: #000; }
ul.dropdown li a                    { display: block; padding: 20px 8px; color: #222; position: relative; z-index: 2000; }
ul.dropdown li a:hover,
ul.dropdown li a.hover              { background: #F3D673; position: relative; }


/* 
    LEVEL TWO
*/
ul.dropdown ul                      { display: none; position: absolute; top: 0; left: 0; width: 180px; z-index: 1000; }
ul.dropdown ul li                   { font-weight: normal; background: #f6f6f6; color: #000; border-bottom: 1px solid #ccc; }
ul.dropdown ul li a                 { display: block; background: #eee !important; } 
ul.dropdown ul li a:hover           { display: block; background: #F3D673 !important; } 
.FixedHeightContainer
{
  float:right;
  height: 250px;
  width:250px;
  overflow: auto;
}
.Content
{

}
li.dropdown-menu-form {
    padding: 5px 10px 0;
    max-height: 100px;
    overflow-y: scroll;
}

#dd-list {
    padding-top: 5px;
    padding-bottom: 5px;
    padding-right: 20px;
    padding-left: 10px;
    font-weight: normal;
    overflow-y: scroll;
}


Corrected CSS:
ul.dropdown-menu-form {
    padding-left: 20px;
    list-style-type: none;
    max-height: 100px;
    overflow-y: scroll;
}

1 个答案:

答案 0 :(得分:0)

JS FIDDLE

<强> HTML

<nav>
<ul class="dropdown">
            <li class="drop"><a href="#">Team Profile</a>
                <ul class="sub_menu">
                            <li><a href="#">Chelsea</a></li>
                <li><a href="#">Man City</a></li>
                <li><a href="#">Southampton</a></li> 
                <li><a href="#">Man Utd</a></li>
                <li><a href="#">Arsenal</a></li>
                <li><a href="#">Liverpool</a></li>
                <li><a href="#">West Ham</a></li>
                <li><a href="#">Newcastle</a></li>
                <li><a href="#">Leichester</a></li>
                <li><a href="#">QPR</a></li>
                <li><a href="#">Burnley</a></li>
                <li><a href="#">Aston Villa</a></li>
                <li><a href="#">Stoke City</a></li>
                <li><a href="#">Crystal Palace</a></li>
                <li><a href="#">Hull City</a></li>
                <li><a href="#">Sunderland</a></li>
                <li><a href="#">Tottenham Hotspur</a></li>
                <li><a href="#">Everton</a></li>
                <li><a href="#">Swansea</a></li>
                <li><a href="#">West Brom</a></li>
                </ul>
            </li>

            </li>
        </ul>
</nav> 

<强> CSS

body{
  margin: 0px;
  padding: 0px;
  background: #000;
  font-family: 'Lato', sans-serif;
}

h1{
  margin: 2em 0px;
  padding: 0px;
  color: #fff;
  text-align: center;
  font-weight: 100;
  font-size: 50px;
}

nav{
  width: 750px;
  margin: 1em auto;
}

ul{
  margin: 0px;
  padding: 0px;
  list-style: none;
}

ul.dropdown{ 
  position: relative; 
  width: 100%; 
}

ul.dropdown li{ 
  font-weight: bold; 
  float: left; 
  width: 180px; 
  position: relative;
  background: #ecf0f1;
}

ul.dropdown a:hover{ 
  color: #000; 
}

ul.dropdown li a { 
  display: block; 
  padding: 20px 8px;
  color: #34495e; 
  position: relative; 
  z-index: 2000; 
  text-align: center;
  text-decoration: none;
  font-weight: 300;
}

ul.dropdown li a:hover,
ul.dropdown li a.hover{ 
  background: #3498db;
  position: relative;
  color: #fff;
}


ul.dropdown ul{ 
 display: none;
 position: absolute; 
  top: 0; 
  left: 0; 
  width: 180px; 
  z-index: 1000;
}

ul.dropdown ul li { 
  font-weight: normal; 
  background: #f6f6f6; 
  color: #000; 
  border-bottom: 1px solid #ccc; 
}

ul.dropdown ul li a{ 
  display: block; 
  color: #34495e !important;
  background: #eee !important;
} 

ul.dropdown ul li a:hover{
  display: block; 
  background: #3498db !important;
  color: #fff !important;
} 

.drop > a{
  position: relative;
}

.drop > a:after{
  content:"";
  position: absolute;
  right: 10px;
  top: 40%;
  border-left: 5px solid transparent;
  border-top: 5px solid #333;
  border-right: 5px solid transparent;
  z-index: 999;
}

.drop > a:hover:after{
  content:"";
   border-left: 5px solid transparent;
  border-top: 5px solid #fff;
  border-right: 5px solid transparent;
}

<强>的Javascript

/*
* 
* Credits to http://css-tricks.com/long-dropdowns-solution/
*
*/

var maxHeight = 400;

$(function(){

    $(".dropdown > li").hover(function() {

         var $container = $(this),
             $list = $container.find("ul"),
             $anchor = $container.find("a"),
             height = $list.height() * 1.1,       // make sure there is enough room at the bottom
             multiplier = height / maxHeight;     // needs to move faster if list is taller

        // need to save height here so it can revert on mouseout            
        $container.data("origHeight", $container.height());

        // so it can retain it's rollover color all the while the dropdown is open
        $anchor.addClass("hover");

        // make sure dropdown appears directly below parent list item    
        $list
            .show()
            .css({
                paddingTop: $container.data("origHeight")
            });

        // don't do any animation if list shorter than max
        if (multiplier > 1) {
            $container
                .css({
                    height: maxHeight,
                    overflow: "hidden"
                })
                .mousemove(function(e) {
                    var offset = $container.offset();
                    var relativeY = ((e.pageY - offset.top) * multiplier) - ($container.data("origHeight") * multiplier);
                    if (relativeY > $container.data("origHeight")) {
                        $list.css("top", -relativeY + $container.data("origHeight"));
                    };
                });
        }

    }, function() {

        var $el = $(this);

        // put things back to normal
        $el
            .height($(this).data("origHeight"))
            .find("ul")
            .css({ top: 0 })
            .hide()
            .end()
            .find("a")
            .removeClass("hover");

    });  

});

JS FIDDLE

致记者:Larry Geams 致意:http://css-tricks.com/long-dropdowns-solution/