如何创建 javascript 函数在鼠标移出时淡出音频?

How to create a javascript function which fades audio out on mouseout?

我有一个非常简单的 javascript 可以在鼠标悬停在图像上时开始音频播放,并在鼠标移开时停止音频播放。

如何更改第二个功能,使其逐渐淡出声音,而不是突然停止?

我看过其他类似的问题,但不幸的是我仍然卡住了。

<img onmouseover="PlaySound('mySound')"
    onmouseout="StopSound('mySound')"
    src="tictactoe.png">

<audio id='mySound' src='audio.mp3'/>

<script>
    function PlaySound(soundobj) {
        var thissound=document.getElementById(soundobj);
        thissound.play();
    }

    function StopSound(soundobj) {
        var thissound=document.getElementById(soundobj);
        thissound.pause();
        thissound.currentTime = 0;
    }
</script>

照原样有效,但添加淡出元素会很好。我知道这是关于将音量设置为随时间减小,但我不确定这样做的正确方法。

您尝试过使用 setInterval() 并将 thissound.volume 递减为 0,然后再递减 pause() 吗?

也许增加声音的开始也是一件好事