请看这个小提琴http://jsfiddle.net/rabelais/yLdkj/1/
上面的小提琴显示了悬停播放音频时的三个小节。如何更改此设置,以便音乐在点击时播放和暂停。此外,如果正在播放一个音频而另一个音频被点击,那么已播放的歌曲如何暂停?
$("#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();
});
答案 0 :(得分:1)
尝试
<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>
然后
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();
}
})
演示:Fiddle
答案 1 :(得分:0)
您可以点击div来检查暂停所有音频,然后只播放相关的音频。你也可以整理一下代码:
<强> jQuery的:强>
$(".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:强>
<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>
答案 2 :(得分:0)
$("#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();
});
}
这很好here。