鼠标悬停时的音频

时间:2010-07-27 10:47:26

标签: jquery html javascript

它部分工作,但如何让以下内容处理多个音频文件:

    <!DOCTYPE HTML>
    <html>
    <body>

    <script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>

    <script type="text/javascript">
    $(function(){
        var birdhover     = $('.birds');
            var birdaudio = birdhover.find('audio')[0];

        birdhover.hover(function(){
           birdaudio.play();
        }, function(){
           birdaudio.stop();
        });
    });
    </script>

    <div class="birds" style="width:30px;height:30px;background-image:url(image1.gif');cursor:pointer;">
       <audio src="sound1.ogg" preload="auto"></audio>
    </div> 

    <div class="birds" style="width:30px;height:30px;background-image:url('/home/imran/Desktop/images/vowel_short_2.gif');cursor:pointer;">
(image2.gif');cursor:pointer;">
       <audio src="sound2.ogg" preload="auto"></audio>
    </div> 

    </body>
    </html>

两个图像都播放相同的声音。

2 个答案:

答案 0 :(得分:6)

如果您不介意使用HTML5

<强> HTML

<div class="birds">
   <audio src="/sounds/bird.mp3" preload="auto"></audio>
</div> 

<强>的Javascript

$(function(){
    var birdhover     = $('.birds');
        var birdaudio = birdhover.find('audio')[0];

    birdhover.hover(function(){
       birdaudio.play();
    }, function(){
       birdaudio.stop();
    });
});

http://www.w3schools.com/html5/tag_audio.asp

答案 1 :(得分:2)

针对多个声音文件进行了更新
这是我之前使用的: http://plugins.jquery.com/project/sound

用法:

$("#sound").sound({swf: url});
$("#sound").load(url);
$("#sound").play();
$("#sound").pause();
$("#sound").stop();
$("#sound").volume(0-100);

取自jQuery插件页面

用于鼠标悬停:

$('#selector').mouseover(function() {
   $("#sound").play('path/to/wav/or/mp3');
});

$('#second-selector').mouseover(function() {
   $("#sound2").play('path/to/second/wav/or/mp3');
});