How to display a range input slider vertically

后端 未结 5 1140
我寻月下人不归
我寻月下人不归 2020-12-02 09:24

I would like to display an slider control vertically. I\'m only concerned with browsers that support the range slider control.

相关标签:
5条回答
  • 2020-12-02 10:07

    .container {
        border: 3px solid #eee;
        margin: 10px;
        padding: 10px;
        float: left;
        text-align: center;
        max-width: 20%
    }
    
    input[type=range].range {
        cursor: pointer;
        width: 100px !important;
        -webkit-appearance: none;
        z-index: 200;
        width: 50px;
        border: 1px solid #e6e6e6;
        background-color: #e6e6e6;
        background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#e6e6e6), to(#d2d2d2));
        background-image: -webkit-linear-gradient(right, #e6e6e6, #d2d2d2);
        background-image: -moz-linear-gradient(right, #e6e6e6, #d2d2d2);
        background-image: -ms-linear-gradient(right, #e6e6e6, #d2d2d2);
        background-image: -o-linear-gradient(right, #e6e6e6, #d2d2d2)
    }
    
    input[type=range].range:focus {
        border: 0 !important;
        outline: 0 !important
    }
    
    input[type=range].range::-webkit-slider-thumb {
        -webkit-appearance: none;
        width: 10px;
        height: 10px;
        background-color: #555;
        background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#4ddbff), to(#0cf));
        background-image: -webkit-linear-gradient(right, #4ddbff, #0cf);
        background-image: -moz-linear-gradient(right, #4ddbff, #0cf);
        background-image: -ms-linear-gradient(right, #4ddbff, #0cf);
        background-image: -o-linear-gradient(right, #4ddbff, #0cf)
    }
    
    input[type=range].round {
        -webkit-border-radius: 20px;
        -moz-border-radius: 20px;
        border-radius: 20px
    }
    
    input[type=range].round::-webkit-slider-thumb {
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        -o-border-radius: 5px
    }
    
    .vertical-lowest-first {
        -webkit-transform: rotate(90deg);
        -moz-transform: rotate(90deg);
        -o-transform: rotate(90deg);
        -ms-transform: rotate(90deg);
        transform: rotate(90deg)
    }
    
    .vertical-heighest-first {
        -webkit-transform: rotate(270deg);
        -moz-transform: rotate(270deg);
        -o-transform: rotate(270deg);
        -ms-transform: rotate(270deg);
        transform: rotate(270deg)
    }
    <div class="container" style="margin-left: 0px">
        <br><br>
        <input class="range vertical-lowest-first" type="range" min="0" max="1" step="0.1" value="1">
        <br><br><br>
    </div>
    
    <div class="container">
        <br><br>
        <input class="range vertical-heighest-first" type="range" min="0" max="1" step="0.1" value="1">
        <br><br><br>
    </div>
    
    
    <div class="container">
        <br><br>
        <input class="range vertical-lowest-first round" type="range" min="0" max="1" step="0.1" value="1">
        <br><br><br>
    </div>
    
    
    <div class="container" style="margin-right: 0px">
        <br><br>
        <input class="range vertical-heighest-first round" type="range" min="0" max="1" step="0.1" value="1">
        <br><br><br>
    </div>

    Source: http://twiggle-web-design.com/tutorials/Custom-Vertical-Input-Range-CSS3.html

    0 讨论(0)
  • 2020-12-02 10:09

    Without changing the position to absolute, see below. This supports all recent browsers as well.

    .vranger {
      margin-top: 50px;
       transform: rotate(270deg);
      -moz-transform: rotate(270deg); /*do same for other browsers if required*/
    }
    <input type="range" class="vranger"/>

    for very old browsers, you can use -sand-transform: rotate(10deg); from CSS sandpaper

    or use

    prefix selector such as -ms-transform: rotate(270deg); for IE9

    0 讨论(0)
  • 2020-12-02 10:13

    You can do this with css transforms, though be careful with container height/width. Also you may need to position it lower:

    input[type="range"] {
       position: absolute;
       top: 40%;
       transform: rotate(270deg);
    }
    <input type="range"/>


    or the 3d transform equivalent:

    input[type="range"] {
       transform: rotateZ(270deg);
    }
    

    You can also use this to switch the direction of the slide by setting it to 180deg or 90deg for horizontal or vertical respectively.

    0 讨论(0)
  • 2020-12-02 10:17

    First, set height greater than width. In theory, this is all you should need. The HTML5 Spec suggests as much:

    ... the UA determined the orientation of the control from the ratio of the style-sheet-specified height and width properties.

    Opera had it implemented this way, but Opera is now using WebKit Blink. As of today, no browser implements a vertical slider based solely on height being greater than width.

    Regardless, setting height greater than width is needed to get the layout right between browsers. Applying left and right padding will also help with layout and positioning.

    For Chrome, use -webkit-appearance: slider-vertical.

    For IE, use writing-mode: bt-lr.

    For Firefox, add an orient="vertical" attribute to the html. Pity that they did it this way. Visual styles should be controlled via CSS, not HTML.

    input[type=range][orient=vertical]
    {
        writing-mode: bt-lr; /* IE */
        -webkit-appearance: slider-vertical; /* WebKit */
        width: 8px;
        height: 175px;
        padding: 0 5px;
    }
    <input type="range" orient="vertical" />


    Disclaimers:

    This solution is based on current browser implementations of as yet undefined or unfinalized CSS properties. If you intend to use it in your code, be prepared to make code adjustments as newer browser versions are released and w3c recommendations are completed.

    MDN contains an explicit warning against using -webkit-appearance on the web:

    Do not use this property on Web sites: not only is it non-standard, but its behavior change from one browser to another. Even the keyword none has not the same behavior on each form element on different browsers, and some doesn't support it at all.

    The caption for the vertical slider demo in the IE documentation erroneously indicates that setting height greater than width will display a range slider vertically, but this does not work. In the code section, it plainly does not set height or width, and instead uses writing-mode. The writing-mode property, as implemented by IE, is very robust. Sadly, the values defined in the current working draft of the spec as of this writing, are much more limited. Should future versions of IE drop support of bt-lr in favor of the currently proposed vertical-lr (which would be the equivalent of tb-lr), the slider would display upside down. Most likely, future versions would extend the writing-mode to accept new values rather than drop support for existing values. But, it's good to know what you are dealing with.

    0 讨论(0)
  • 2020-12-02 10:24

    Its very simple. I had implemented using -webkit-appearance: slider-vertical, It worked in chorme, Firefox, Edge

    <input type="range">
    
    input[type=range]{
        writing-mode: bt-lr; /* IE */
        -webkit-appearance: slider-vertical; /* WebKit */
        width: 50px;
        height: 200px;
        padding: 0 24px;
        outline: none;
        background:transparent;
    }
    
    0 讨论(0)
提交回复
热议问题