html select scroll bar

后端 未结 2 484
悲哀的现实
悲哀的现实 2020-12-05 12:04

how do you add a scroll bar to the html select box? is there any javascript library that emulate this behavior?

or is there anyway to redesign, the interface I have

相关标签:
2条回答
  • 2020-12-05 12:33

    Horizontal scrollbars in a HTML Select are not natively supported. However, here's a way to create the appearance of a horizontal scrollbar:

    1. First create a css class

    <style type="text/css">
     .scrollable{
       overflow: auto;
       width: 70px; /* adjust this width depending to amount of text to display */
       height: 80px; /* adjust height depending on number of options to display */
       border: 1px silver solid;
     }
     .scrollable select{
       border: none;
     }
    </style>
    

    2. Wrap the SELECT inside a DIV - also, explicitly set the size to the number of options.

    <div class="scrollable">
    <select size="6" multiple="multiple">
        <option value="1" selected>option 1 The Long Option</option>
        <option value="2">option 2</option>
        <option value="3">option 3</option>
        <option value="4">option 4</option>
        <option value="5">option 5 Another Longer than the Long Option ;)</option>
        <option value="6">option 6</option>
    </select>
    </div>
    
    0 讨论(0)
  • 2020-12-05 12:36

    One options will be to show the selected option above (or below) the select list like following:

    HTML

    <div id="selText"><span>&nbsp;</span></div><br/>
    <select size="4" id="mySelect" style="width:65px;color:#f98ad3;">
        <option value="1" selected>option 1 The Long Option</option>
        <option value="2">option 2</option>
        <option value="3">option 3</option>
        <option value="4">option 4</option>
        <option value="5">option 5 Another Longer than the Long Option ;)</option>
        <option value="6">option 6</option>
    </select>
    

    JavaScript

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"   
      type="text/javascript"></script> 
    <script type="text/javascript">
    $(document).ready(function(){        
        $("select#mySelect").change(function(){
          //$("#selText").html($($(this).children("option:selected")[0]).text());
           var txt = $($(this).children("option:selected")[0]).text();
           $("<span>" + txt + "<br/></span>").appendTo($("#selText span:last"));
        });
    });
    </script>
    

    PS:- Set height of div#selText otherwise it will keep shifting select list downward.

    0 讨论(0)
提交回复
热议问题