Get the value on hover of an input type range

前端 未结 1 1137
灰色年华
灰色年华 2020-12-19 13:46

I want to create a seek bar for a video player. On mousemove on the slider I want to get the current value.

I expect ValueHover to equal

相关标签:
1条回答
  • 2020-12-19 14:24

    capture mouseup event on seekslider and assign valueHover value to seekslider

    var valueHover = 0;
    function calcSliderPos(e) {
        return (e.offsetX / e.target.clientWidth) *  parseInt(e.target.getAttribute('max'),10);
    }
    
    //attach to slider and fire on mousemove
    document.getElementById('seekslider').addEventListener('mousemove', function(e) {
        valueHover = calcSliderPos(e).toFixed(2);
        document.getElementById('durtimeText').innerHTML = valueHover;
    });
    document.getElementById('seekslider').addEventListener('mouseup', function(e) {
        valueHover = calcSliderPos(e).toFixed(2);
     valueHover= valueHover>100?100:valueHover;
      valueHover= valueHover<0?0:valueHover;
        document.getElementById('seekslider').value = valueHover;
      document.getElementById('seek').innerHTML = valueHover;
    });
    
    document.getElementById('seekslider').addEventListener('change', function(e) {
        var valueSeeked = e.target.value;
        document.getElementById('seek').innerHTML = valueSeeked;
        document.getElementById('test').innerHTML = valueSeeked === valueHover;
    });
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    </head>
    <body>
    
    <input id="seekslider" type="range" min="0" max="100" value="0" step="0.01" style="width:300px;"> 
    <br/>
    valueHover : <span id="durtimeText"></span><br/>
    valueSeeked :  <span id="seek"></span><br/>
    valueHover expect to be equal valueSeeked : <span id="test"></span><br/>
    </body>
    </html>

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