我用纯 JavaScript 开发了一个音量控制器。鼠标移动工作正常,但不幸的是我无法使其成为完美的音量控制器。该功能面临两个问题:
鼠标点击音量不变
onmousemove
功能即使在之后继续工作onmouseup
var vControl = document.getElementById("voumecontrol"); var vNow = document.getElementById("volumenow"); var resultDiv = document.getElementById("result"); vControl.onmousedown = function (e) { vControl.onmousemove = function (e) { var varPosition = e.pageY - vControl.offsetTop; var volPercentage = varPosition / vControl.offsetHeight; kili = Math.abs((e.pageY - (vControl.offsetTop + vControl.offsetHeight)) / vControl.offsetHeight); var volumePer = kili * 100; vNow.style.height = volumePer + "%"; resultDiv.innerHTML = "volume position " + volumePer + "%"; } vControl.onmouseup = function (e) { vControl.onmousemove = function (e) { e.preventDefault(); } } }