Javascript在悬停时播放声音.在hoveroff上停止并重置

use*_*357 5 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();
}
Run Code Online (Sandbox Code Playgroud)

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

onmouseover="EvalSound('sound1')" onmouseout="StopSound('sound1')"
Run Code Online (Sandbox Code Playgroud)

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

zev*_*vdg 6

<embed>标签是嵌入多媒体的旧方式.你真的应该使用新的HTML5 <audio>或<video>标签,因为它们是嵌入多媒体对象的首选和标准化方式.您可以使用HTMLMediaElement界面来播放,暂停和搜索媒体(以及更多内容).

这是一个在mouseover上播放音频文件并在mouseout上停止播放的简单示例.

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'/>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

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

function StopSound(soundobj) {
    var thissound=document.getElementById(soundobj);
    thissound.pause();
    thissound.currentTime = 0;
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看MDN指南以嵌入音频和视频