gpt4 book ai didi

javascript - 单击时切换音频?

转载 作者:行者123 更新时间:2023-11-28 01:49:36 26 4
gpt4 key购买 nike

请看看这个 fiddle http://jsfiddle.net/rabelais/yLdkj/1/

上面的 fiddle 显示了悬停时播放音频的三个栏。我如何更改此设置,以便音乐在单击时播放和暂停。另外,如果一个音频正在播放,而另一个音频被点击,已经播放的歌曲如何暂停?

 $("#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();
});

最佳答案

尝试

<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

关于javascript - 单击时切换音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19855381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com