2012-03-08 86 views
5

我正在嘗試將滾動條添加到下拉菜單選項,因此當用戶單擊菜單時,它將不會一直顯示所有選項直到結束。我試過在下拉菜單選項上添加滾動條

<select name='menu'> 
    <option value='1'>first item</option> 
    <option value='2'>second item</option> 
    <option value='3'>third item</option> 
    <option value='4'>fourth item</option> 
    <option value='5'>fifth item</option> 
    <option>........ 
    <option>........ 
    //I have many options..... 
</select> 

我嘗試這個CSS,但它只能在菜單本身,而不是選項。

select { 
height:50px; 
overflow-y: scroll; 
} 

有什麼想法?非常感謝。

+1

我覺得這是你的答案:http://stackoverflow.com/questions/5715705/always-show-vertical-scrollbar-in-select – koenpeters 2012-03-08 20:41:42

+0

太棒了!非常感謝。 +1 – FlyingCat 2012-03-08 21:49:07

回答

4

這也是我很好的HANDELING吧:)

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"); 

}); 

// Add down arrow only to menu items with submenus 
$(".dropdown > li:has('ul')").each(function() { 
    $(this).find("a:first").append("<img src='images/down-arrow.png' />"); 
}); 

});

+0

很酷。謝謝您的幫助。 – FlyingCat 2012-03-08 21:50:27

+0

滾動速度非常快,如何控制它 – Tarun 2013-07-21 08:15:53

+0

@Tarun我建議你爲此創建一個新問題。創建一個js小提琴(jsfiddle.net),我相信你很快就會得到一個很好的答案! – 2013-07-23 13:22:24

8

給一個CSS的選擇像class="myDropDown"並添加以下CSS

.myDropDown 
{ 
    height: 50px; 
    overflow: auto; 
} 

供參考:fiddle

0
<style type="text/css"> 
      /*dropdown menu code start*/ 
      @media only screen and (min-width:769px) { 
       .dropdown:hover .dropdown-menu { 
        display: block; 
       } 
       .dropdown-submenu { 
        position: relative !important; 
       } 

       .dropdown-submenu>.dropdown-menu { 
        top: 0 !important; 
        left: 100% !important; 
        margin-top: -6px !important; 
        margin-left: -1px !important; 
        border-radius: 0 !important; 
       } 

       .dropdown-submenu:hover>.dropdown-menu { 
        display: block !important; 
       } 

       .dropdown-submenu>a:after { 
        display: block; 
        content: "\f105"; 
        font-family: 'FontAwesome'; 
        margin-top: -18px; 
        right: 15px; 
        position: absolute; 
        font-weight: 300; 
       } 
       .customcaret{ 
       float: right; 
       } 
      } 
      /*dropdown menu code start*/ 

     .navbar-default .navbar-nav > li > a { 
      color: #535353; 
      transition: all ease 0.5s; 
      -webkit-transition: all ease 0.5s; 
      -moz-transition: all ease 0.5s; 
      font-size: 1.15em !important; 
      text-transform: uppercase; 
     } 
     </style> 
+0

你能解釋一下爲什麼它是好的,你的解決方案是基於什麼等? – Massimo 2017-02-13 07:43:24

+0

請簡要解釋這段代碼做了什麼。只有代碼答案不是很有幫助。 – Jeet 2017-02-13 07:55:32

相關問題