我想为视频播放器创建搜索栏。在滑块上的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/>
答案 0 :(得分:2)
在seeklider上捕获mouseup
事件并将valueHover值分配给seeklider
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;