audio in hidden div

0

I have a div with display: none and this div has an audio, but if I use autoplay, even if the div is hidden, the audio stays playing, I would like it to play the audio only when the div is appearing. Can you help me?

<div class="fig1"></div>

.fig1{display:none}
.evnt1{display:block}

<script>$('.fig1').click(function(){
$('.fig1').show('slow');});
</script>
    
asked by anonymous 09.07.2017 / 06:29

2 answers

2

Try to put id in your audio and do as follows:

<audio id="myAudio" src="some_audio.mp3"></a>
<div class="fig1"></div>

.fig1{display:none}
.evnt1{display:block}

<script>$('.fig1').click(function(){
    $('.fig1').show('slow', function(){
        document.getElementById('myAudio').play();
    });});
</script>
    
10.07.2017 / 15:14
1

You can get the dsplay value of your div with the code below then use an if to determine the action on the audio.

var display = $('.fig1').css('display');
var som =$(soundobj);
if(display == 'none'){
   som.stop();
   // para o audio.
}else som.play();

I did not make conferences about the code to suit your needs Good luck!

    
09.07.2017 / 06:36