Show tick positions in custom range input

后端 未结 3 445
萌比男神i
萌比男神i 2020-12-30 13:37

Please take a look at the following CodePen. There you can see my custom range input. I want the tick positions of the slider to be shown and for that I added the datalist:<

相关标签:
3条回答
  • 2020-12-30 14:21

    I know my answer is way way late, but I keep coming back here when I try to find doing the same thing. I did manage to display tickmarks using the following settings.

    The code results in the following:

    * {
      box-sizing: border-box;
    }
    
    .slider {
      -webkit-appearance: none;
      appearance: none;
      width: 100%;
      height: 25px;
      background: #D3D3D3;
      outline: none;
      opacity: 0.7;
      -webkit-transition: .2s;
      transition: opacity .2s;
    }
    
    .slider:hover {
      opacity: 1;
    }
    
    .slider::-webkit-slider-thumb {
      -webkit-appearance: none;
      appearance: none;
      width: 25px;
      height: 25px;
      background: #FF0000;
      cursor: pointer;
    }
    
    .slider::-moz-range-thumb {
      width: 25px;
      height: 25px;
      background: #FF0000;
      cursor: pointer;
    }
    
    .sliderticks {
      display: flex;
      justify-content: space-between;
      padding: 0 10px;
    }
    
    .sliderticks p {
      position: relative;
      display: flex;
      justify-content: center;
      text-align: center;
      width: 1px;
      background: #D3D3D3;
      height: 10px;
      line-height: 40px;
      margin: 0 0 20px 0;
    }
    <div class="range">
      <input type="range" min="0" max="5" value="2" class="slider">
      <div class="sliderticks">
        <p>0</p>
        <p>1</p>
        <p>2</p>
        <p>3</p>
        <p>4</p>
        <p>5</p>
      </div>
    </div>

    0 讨论(0)
  • 2020-12-30 14:22

    document.querySelectorAll(".__range-step").forEach(function(ctrl) {
    	var el = ctrl.querySelector('input');        
    	var output = ctrl.querySelector('output'); 
    	var newPoint, newPlace, offset;
    	el.oninput =function(){ 
    		// colorize step options
    		ctrl.querySelectorAll("option").forEach(function(opt) {
    			if(opt.value<=el.valueAsNumber)                
    				opt.style.backgroundColor = '#48530d';
    			else
    				opt.style.backgroundColor = '#a4b162';
    		});           
    	};
    	el.oninput();    
    });
    .__range input
    {
      outline: none;
    	-webkit-appearance: none;
    	background-color: #aaa;
    	height: 3px;
    	width: 100%;
    	margin: 10px auto;
    }
    input[type=range]::-webkit-slider-runnable-track {
        width: 100%;
        height: 8px;
        cursor: pointer;
        box-shadow: 0px 0px 0px rgba(0, 0, 0, 0), 0px 0px 0px rgba(13, 13, 13, 0);
        background: #a4b162;
        border-radius: 0px;
        border: 1px solid rgba(0, 0, 0, 0);
    }
    input[type=range]::-webkit-slider-thumb {
        box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
        border: 1px solid #000000;
        height: 16px;
        width: 16px;
        border-radius: 0px;
        background: #48530d;
        cursor: pointer;
        -webkit-appearance: none;
        margin-top: -5px;
    }
    
    .__range-step{
    	position: relative;                
    }
    
    .__range-max{
    	float: right;
    }
    .__range-step datalist {
    	position:relative;
    	display: flex;
    	justify-content: space-between;
    	height: auto;
    	bottom: 16px;
    	/* disable text selection */
    	-webkit-user-select: none; /* Safari */        
    	-moz-user-select: none; /* Firefox */
    	-ms-user-select: none; /* IE10+/Edge */                
    	user-select: none; /* Standard */
    	/* disable click events */
    	pointer-events:none;  
    }
    .__range-step datalist option {
    	width: 10px;
    	height: 10px;
    	min-height: 10px;
      padding:0;
      line-height: 40px;
    }
    
    .__range{
      margin:10px 40px;
    }
    <div class="__range">
          <input value="4" type="range" max="6" min="1" step="1" list="ticks1">
    </div>
    
    <div class="__range __range-step">
            <input value="4" type="range" max="6" min="1" step="1" list="ticks1">
            <datalist id="ticks1">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
            </datalist>
    </div>

    0 讨论(0)
  • 2020-12-30 14:30
    -webkit-appearance: none;
    

    The above removes platform-native styling (Chrome / Safari), which removes the ticks.

    You can get more information here, but unfortunately, styling support for input ranges (particularly ticks) is still pretty terrible.

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