如何使用 vanilla js(无 jquery)淡出音频元素?我查看了这个论坛上的许多其他问题,但它们似乎都过于复杂或使用 jquery。我在下面写了这段代码。但是,我收到以下错误:“未捕获的 DOMException:无法在 'HTMLMediaElement' 上设置 'volume' 属性:提供的音量 (-7.52870e-16) 超出范围 [0, 1]。”
viewer.on('mouseup', function(event) {
var mouseDownSound = document.getElementById("mouseDownSound");
mouseDownSound.volume = 1;
var vol = mouseDownSound.volume;
function fadeOut() {
setInterval(
function() {
if (vol > 0) {
vol -= 0.01;
mouseDownSound.volume = vol;
}
else {
clearInterval(fadeOut);
}
}, 2);
}
fadeOut();
});