我正在学习一些初学者的前端开发。我创建了一个非常简单的网站,该网站利用了Lightbox工具包。我已经弄清楚了当有人单击缩略图但需要一些有关如何停止它的指针时如何开始音轨的方法,或者为了获得更可靠的解决方案,如果有人单击“下一个”箭头,则播放适当的音轨。到目前为止,这是我的代码:
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/lightbox.min.css">
<script src="js/lightbox-plus-jquery.min.js"></script>
<meta charset="utf-8">
<title>verenti ltd</title>
<meta name="viewport" content="width-device-width, initial-scale=1.0">
<meta name="author" content="SitePoint">
</head>
<body bgcolor="#F7FBED ">
<div class="gallery">
<a href="images/drake_big.jpg" data-lightbox="mygallery" data-title="nick drake"
onclick=PlaySound("music/drake.mp3")><img src="images/drake_small.jpg"></a>
<a href="images/prine_big.jpg" data-lightbox="mygallery" data-title="john prine"
onclick=PlaySound("music/prine.mp3")> <img src="images/prine_small.jpg"></a>
<a href="images/westerberg_big.jpeg" data-lightbox="mygallery" data-title="paul westerberg"
onclick=PlaySound("music/westerberg.mp3")> <img src="images/westerberg_small.jpg"></a>
<a href="images/townes_big.png" data-lightbox="mygallery" data-title="townes van zandt"
onclick=PlaySound("music/townes.mp3")> <img src="images/townes_small.jpg"></a>
<script>
function PlaySound(path) {
//this function will work like a toggler for sound track playing
var sound = document.getElementById(path);
if(sound && sound.currentTime > 0){ //check whether it is already playing
sound.pause();// stop the sound
sound.currentTime = 0 //
}else if(sound){
//this block to play paused sound track
sound.play();
}else{
//this block to play new sound track
sound= document.createElement('audio');
sound.setAttribute('src', path);
sound.setAttribute('id', path);
sound.play();
}
}
</script>
</div>
</body>
</html>
我想我可以更好地利用标签?
答案 0 :(得分:1)
一个简单的解决方案是:
<script>
function PlaySound(path) {
//this function will work like a toggler for sound track playing
var sound = document.getElementById(path);
if(sound && sound.currentTime > 0){ //check whether it is already playing
sound.pause();// stop the sound
sound.currentTime = 0 //
}else if(sound){
//this block to play paused sound track
sound.play();
}else{
//this block to play new sound track
sound= document.createElement('audio');
sound.setAttribute('src', path);
sound.setAttribute('id', path);
sound.play();
}
}
</script>