magnific-popup,弹出窗口时如何播放音频文件,关闭弹出窗口时停止播放

时间:2016-08-22 20:50:23

标签: jquery magnific-popup

我正在使用magnific-popup来显示图片...... 弹出窗口打开时是否可以开始播放au音频文件,关闭窗口时是否可以停止播放?

<a href="images/capoeira.jpg" class="fh5co-project-item image-popup to-animate">
    <img src="images/berimbau.jpg" alt="Animason groupes" class="img-responsive">
    <div class="fh5co-text">
        <h1>ANIMA'SON</h1>
        <span>Participants: minimum 10, maximum: 20</span>
    </div>
</a>

1 个答案:

答案 0 :(得分:0)

我通过在每个图片弹出链接

中添加音频标签来解决它
<a href="images/capoeira.jpg" data-sound="berimbau" class="fh5co-project-item image-popup to-animate">
    <img src="images/berimbau.jpg"  alt="Animason groupes" class="img-responsive">
    <div class="fh5co-text">
        <h1>ANIMA'SON</h1>
        <h2>60€ / heure / groupe</h2>
        <span>Participants: minimum 10, maximum: 20</span>
    </div>
    <audio  id="berimbau-audio" loop="loop" preload="none">  
        <source src="./sounds/berimbau.ogg" type="audio/ogg">
        <source src="./sounds/berimbau.mp3" type="audio/mpeg">
    </audio>
</a>

使用data-sound参数和audio.id将回调添加到magnific-popup-options.js中

$(document).ready(function() {
    var soundName;
    var audioElement;

    // MagnificPopup  
    $('.image-popup').magnificPopup({
        ....
        callbacks: {
            elementParse: function(item) {
                soundName = item.el[0].attributes['data-sound'].value;
                audioElement= document.getElementById(soundName + "-audio")
                audioElement.load();
            },
            close: function() {
                audioElement.pause();
            },
            change: function() {
                audioElement.play();
            }
        },
    });
});