Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 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 Reply

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...