【发布时间】:2014-07-15 03:50:21
【问题描述】:
我试图在我的网页上创建一个音频预览页面,其中每个音频元素都有自己的进度条。但是,我遇到了麻烦,让每个音频元素仅针对其进度条。我对此很陌生,所以我希望这不是一个直截了当的愚蠢问题,但我非常感谢您的帮助,提前致谢。
这是每首歌曲预览的 html。
<audio id="player"></audio>
<div class="songPreview">
<div class="disp">
<div class="button" data-src="EXAMPLE.wav" data-pos="0">
<a href="#" class="soundPlay"><img src="Buttons/play.png"></a>
<a href="#" class="soundStop hidden"><img src="Buttons/stop.png"></a>
</div>
<div class="songInfo">
<p>SONG NAME HERE</p>
</div>
</div>
<progress class="seekbar" value="0" max="1" style="width:250px"></progress>
</div>
<div class="songPreview2">
<div class="disp">
<div class="button" data-src="EXAMPLE2.wav" data-pos="0">
<a href="#" class="soundPlay"><img src="Buttons/play.png"></a>
<a href="#" class="soundStop hidden"><img src="Buttons/stop.png"></a>
</div>
<div class="songInfo">
<p>SONG NAME HERE</p>
</div>
</div>
<progress class="seekbar" value="0" max="1" style="width:250px"></progress>
</div>
这就是 JS
$(document).ready(function() {
var myAudio = document.getElementById("player");
//play stop button etc.
$(document).on('click', '.soundPlay', function(e) {
var target = this;
$(this).hide();
$(this).next("a").show();
myAudio.src = target.parentNode.getAttribute('data-src');
myAudio.play();
e.preventDefault();
});
$(document).on('click', '.soundStop', function(e) {
var target = this;
$(this).hide();
$(this).prev("a").show();
$('audio').each(function(){
this.pause();
this.currentTime = 0;
});
e.preventDefault();
});
//end of play stop button
//seekbar
$('#player').on('timeupdate', function() {
$('.seekbar').attr("value", this.currentTime / this.duration);
});
//end seek bar
//auto switch button on stop
var audio = document.getElementById('player');
audio.addEventListener('ended', stopAudio);
function stopAudio() {
audio.stop;
$('.soundPlay').show('slow');
$('.soundStop').hide('fast');
$('.seekbar').attr(this.currentTime = 0);
}
});
我似乎无法弄清楚如何仅针对每个相应 div 中的进度标签。 无论如何,我可以在没有一百万个不同的进度 ID 和过度重复的 JS 代码的情况下做到这一点吗? (因为一个页面上会有 100 首这样的歌曲预览)
提前致谢!
【问题讨论】:
-
我只看到一个
audio元素。您如何将src分配给audio。贴出整个JS代码。 -
您的标记有些混乱。您有一个音频元素#player,但您有两个 div,每个 div 都有一个进度元素。你能澄清一下吗?
-
对不起那些家伙,我用整个 JS 代码更新了这个问题。基本上,音频标签是在 div 之外定义的,并从按钮类中的 data-src 获取其数据。但是,一旦播放了任何歌曲,所有进度条都会移动,但我只需要定位单个 songPreview div 中的进度条。感谢 ShaunakD 和 @user3558931
标签: javascript jquery audio progress-bar progress