Javascript 在悬停时播放声音.在悬停时停止并重置 [英] Javascript play sound on hover. stop and reset on hoveroff

查看:25
本文介绍了Javascript 在悬停时播放声音.在悬停时停止并重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

This is my code to play a audio file,

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

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

It is currently working on hover, however when i go back to the image that it plays under it doesnt go back to the beginning, it continues playing

推荐答案

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

The <embed> tag is the old way to embed multimedia. You really ought to be using the new HTML5 <audio> or <video> tags as they are the preferred and standardized way to embed multimedia objects. You can use the HTMLMediaElement interface to play, pause, and seek through the media (and lots more).

这是一个简单示例,它在鼠标悬停时播放音频文件并在鼠标悬停时停止播放.

Here is a simple example that plays an audio file on mouseover and stops it on 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'/>

Javascript:

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 指南

这篇关于Javascript 在悬停时播放声音.在悬停时停止并重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆