javascript - 在鼠标单击事件上停止音轨

标签 javascript jquery html html5-audio lightbox

我正在学习一些初学者前端开发。我创建了一个非常简单的网站,它使用 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>

我认为我可以更好地利用该标签?

最佳答案

一个简单的解决方案是:

    <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>

关于javascript - 在鼠标单击事件上停止音轨,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359802/

相关文章:

javascript - 使文本在图像的中心对齐

javascript - 始终在 React 应用程序中获取以前的状态

javascript - 类型错误 : Cannot call a class as a function after deploying to Netlify?

jquery - 在 jqGrid 的版本中更改单元格的编辑类型

jquery - 在窗口大小更改时加载 CloudZoom

javascript - 我怎么知道最后一次异步何时完成?

javascript - 修改基础 form.reset() 值

javascript - 如何在鼠标悬停时从占位符中删除图像

javascript - 在包含在对象中的文本(字符串)周围添加跨度标签

javascript - 如何在 .vue 文件中导入包