magnific-popup, how to play an audio file when pop

2019-09-15 23:44发布

问题:

I am using magnific-popup to display pictures ... Is it possible to start playing au audio file when the popup window open and stop it when closing the window ?

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

I solved it by adding an audio tag within each image-popup link

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

and adding callbacks into he magnific-popup-options.js, using the data-sound parameter and audio.id

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