首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >点击切换音频?

点击切换音频?
EN

Stack Overflow用户
提问于 2013-11-08 09:18:56
回答 3查看 131关注 0票数 1

请看这把小提琴http://jsfiddle.net/rabelais/yLdkj/1/

上面的小提琴显示了三个酒吧,在悬停播放音频。我如何改变这一点,使音乐播放和停顿点击代替。另外,如果一个声音在播放,另一个被点击,那么已经播放的歌曲怎么能暂停呢?

代码语言:javascript
复制
 $("#one").mouseenter(function () {
 $('#sound-1').get(0).play();
 });
$("#one").mouseleave(function () {
$('#sound-1').get(0).pause();
});
$("#two").mouseenter(function () {
$('#sound-2').get(0).play();
});
$("#two").mouseleave(function () {
$('#sound-2').get(0).pause();
});
$("#three").mouseenter(function () {
$('#sound-3').get(0).play();
});
$("#three").mouseleave(function () {
$('#sound-3').get(0).pause();
});
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-11-08 09:38:06

试一试

代码语言:javascript
复制
<div id="sound-bars">
    <div id="one" class="bars" data-target="#sound-1"></div>
    <div id="two" class="bars" data-target="#sound-2"></div>
    <div id="three" class="bars" data-target="#sound-3"></div>
</div>
<audio id="sound-1" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-2" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/you did not listen .mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/you did not listen .oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-3" class="sound">
    <source src="http://angelaandtom.co.uk/lib/audio/can you hear me .mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/can you hear me .oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>

然后

代码语言:javascript
复制
var $sounds = $('.sound'),
    $bars = $('#sound-bars .bars');
$bars.click(function () {
    var $this = $(this),
        $target = $($this.data('target')),
        target = $target[0];
    $bars.not(this).removeClass('playing');
    $sounds.not($target).each(function () {
        this.pause();
    });

    $this.toggleClass('playing');
    if ($this.hasClass('playing')) {
        target.play();
    } else {
        target.pause();
    }
})

演示:小提琴

票数 1
EN

Stack Overflow用户

发布于 2013-11-08 09:28:15

您可以检查暂停所有音频点击一个div,然后只播放相关的音频。您也可以整理代码:

jQuery:

代码语言:javascript
复制
$(".playbtn").click(function() {
    // Pause all audio
    $("audio").each(function(index, elem) {
        elem.pause();
    });
    // Grab the associated sound ID
    var soundId = $(this).data("soundid");
    // Play the audio
    $("#sound-"+soundId)[0].play();
});

HTML:

代码语言:javascript
复制
<div class="playbtn" data-soundid="1">Play #1</div>
<div class="playbtn" data-soundid="2">Play #2</div>
<div class="playbtn" data-soundid="3">Play #3</div>
<div class="playbtn" data-soundid="4">Play #4</div>

<audio id="sound-1">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-2">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-3">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
<audio id="sound-4">
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.mp3"></source>
    <source src="http://angelaandtom.co.uk/lib/audio/Intro-.oggvorbis.ogg"></source>
    Your browser isn't compatible.
</audio>
票数 0
EN

Stack Overflow用户

发布于 2013-11-08 09:29:23

代码语言:javascript
复制
$("#one").click(function () {
  pauseAll();
    $('#sound-1').get(0).play();
});

$("#two").click(function () {
pauseAll();         
    $('#sound-2').get(0).play();
});

$("#three").click(function () {
      pauseAll();
    $('#sound-3').get(0).play();
});
function pauseAll(){
  $("audio").each(function (){
        this.pause();
    });
}

这是一个很好的这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19855381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档