Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

javascript - Get the value on hover of an input type range

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 ValueSeeked. How can I validate my test?

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('change', function(e) {
    var valueSeeked = e.target.value;
    document.getElementById('seek').innerHTML = valueSeeked;
    document.getElementById('test').innerHTML = valueSeeked === valueHover;
});
<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/>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...