magnific-popup, how to play an audio file when popup open and stop playing when closing it

梦想的初衷 提交于 2019-12-12 03:48:29

问题


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


来源:https://stackoverflow.com/questions/39088518/magnific-popup-how-to-play-an-audio-file-when-popup-open-and-stop-playing-when

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!