HTML5 type=range - showing label

后端 未结 7 2072
心在旅途
心在旅途 2021-02-12 13:25

Is there a way I can also set some label text for each steps in the HTML5 type=range control. Basically I have a range control

相关标签:
7条回答
  • 2021-02-12 13:38

    There is no native way of doing it. And as input[type=range] is very poorly supported, I will recommend using jQuery UI slider and the way of attaching labels found here in answer.

    0 讨论(0)
  • 2021-02-12 13:42

    I've put together for you.

    // define a lookup for what text should be displayed for each value in your range
    var rangeValues =
    {
        "1": "You've selected option 1!",
        "2": "...and now option 2!",
        "3": "...stackoverflow rocks for 3!",
        "4": "...and a custom label 4!"
    };
    
    
    $(function () {
    
        // on page load, set the text of the label based the value of the range
        $('#rangeText').text(rangeValues[$('#rangeInput').val()]);
    
        // setup an event handler to set the text when the range value is dragged (see event for input) or changed (see event for change)
        $('#rangeInput').on('input change', function () {
            $('#rangeText').text(rangeValues[$(this).val()]);
        });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="range" id="rangeInput" name="rangeInput" step="1" min="1" max="4">
    <label id="rangeText" />

    0 讨论(0)
  • 2021-02-12 13:45

    You can use jSlider. Its a jQuery slider plugin for range inputs.

    https://github.com/egorkhmelev/jslider

    Just check out the demos and documentation. Hope this helps.

    0 讨论(0)
  • 2021-02-12 13:47

    I guess the easiest solution (plain Javascript) is:

    <fieldset>
        <label for="rangeVal">resolution (dpi):</label>
        <input type ="range" max="1000" min="20"
            oninput="document.getElementById('rangeValLabel').innerHTML = this.value;"
            step="1" name="rangeVal" id="rangeVal" value="200">
        </input>
        <em id="rangeValLabel" style="font-style: normal;"></em>
    </fieldset>
    

    This code does not need jQuery nor CSS and should work on any browser that supports the range input type.

    0 讨论(0)
  • 2021-02-12 13:48

    OP,

    I put together a demo that uses a range input with corresponding <p> tags that act as both labels for the current state of the slider, as well as triggers to change the slider's value.

    Plunk

    http://plnkr.co/edit/ArOkBVvUVUvtng1oktZG?p=preview.


    HTML Markup

    <div class="rangeWrapper">
        <input id="slide" type="range" min="1" max="4" step="1" value="1"  /> 
        <p class="rangeLabel selected">Label A</p>
        <p class="rangeLabel">Label B</p>
        <p class="rangeLabel">Label C</p>
        <p class="rangeLabel">Label D</p>
    </div>
    

    Javascript

    $(document).ready(function(){
    
      $("input[type='range']").change(function() {
        slider = $(this);
        value = (slider.val() -1);
    
        $('p.rangeLabel').removeClass('selected');
        $('p.rangeLabel:eq(' + value + ')').addClass('selected');
    
      });
    
      $('p.rangeLabel').bind('click', function(){
        label = $(this);
        value = label.index();
        $("input[type='range']").attr('value', value)
                                .trigger('change');
      });
    
    });
    

    CSS

    input[type="range"] {
        width: 100%;
    }
    
    p.rangeLabel {
        font-family: "Arial", sans-serif;
        padding: 5px;
        margin: 5px 0;
        background: rgb(136,136,136);
        font-size: 15px;
        line-height 20px;
    }
    
    p.rangeLabel:hover {
        background-color: rgb(3, 82, 3);
        color: #fff;
        cursor: pointer;
    }
    
    p.rangeLabel.selected {
        background-color: rgb(8, 173, 8);
        color: rgb(255,255,255);
    }
    

    Also worth nothing, if you're interested in showing the current value as a label/flag to the user, (instead of many) there's a great article by Chris Coyier on value bubbles for range sliders.

    0 讨论(0)
  • 2021-02-12 13:50

    FWIW the standard (HTML 5.1, HTML Living Standard), specifies a label attribute for options when using a datalist. Sample code here.

    This isn't implemented by any browser yet.

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