获取输入类型范围的悬停值

时间:2016-02-25 15:29:40

标签: javascript html

我想为视频播放器创建搜索栏。在滑块上的mousemove上,我想获取当前值。

我希望ValueHover等于ValueSeeked。我该如何验证我的测试?

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/>

1 个答案:

答案 0 :(得分:2)

在seeklider上捕获mouseup事件并将valueHover值分配给seeklider

&#13;
&#13;
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;
});
&#13;
<!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>
&#13;
&#13;
&#13;