Javascript 在悬停时播放声音。在悬停时停止并重置

标签 javascript audio onmouseover onmouseout onhover

function EvalSound(soundobj) {
    var thissound=document.getElementById(soundobj);
    thissound.currentTime = 0;  
    thissound.Play();
}

function StopSound(soundobj) {
    var thissound=document.getElementById(soundobj);
    thissound.Stop();
}

这是我播放音频文件的代码,

onmouseover="EvalSound('sound1')" onmouseout="StopSound('sound1')"

它目前正在悬停,但是当我回到它播放的图像时它不会回到开头,它会继续播放

最佳答案

标签是嵌入多媒体的旧方法。您真的应该使用新的 HTML5

这是一个simple example在鼠标悬停时播放音频文件并在鼠标移开时停止。

HTML:

<p onmouseover="PlaySound('mySound')" 
    onmouseout="StopSound('mySound')">Hover Over Me To Play</p>

<audio id='mySound' src='http://upload.wikimedia.org/wikipedia/commons/6/6f/Cello_Live_Performance_John_Michel_Tchaikovsky_Violin_Concerto_3rd_MVT_applaused_cut.ogg'/>

Javascript:

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

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

有关更多信息,请查看 MDN guide for embedding audio and video

关于Javascript 在悬停时播放声音。在悬停时停止并重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14926306/

相关文章:

ios - 当主线程使用量增加时,IOS音频线程CPU使用率下降

javascript - 在 JSX 中悬停时更改图像

javascript - d3.on ("mouseover") 在鼠标悬停之前发生

javascript - 当鼠标不在元素上时,使用什么事件代替 onmouseout 运行?

ios - 如何在屏幕锁定/后台模式下播放音频

javascript - 基于其位置的可拖动 DIV 样式

javascript - 如何将文件上传文本更改为图像并上传文件并渲染到 div

以参数作为对象的 Javascript 函数

javascript - 在 jQuery 中组合多个选择器

objective-c - AudioStreamBasicDescription 中是否需要 mBytesPerFrame?