html5 - I have an image I would like to use to control an <audio> source. I can't find a way to use both the pause and play function simultaneously -
so, have audio track autoplay when opening website
<audio class="topper" autoplay loop id="minuscolo"> <source src="media/minuscolo.mp3" type="audio/mpeg"> browser not support audio element. </audio>
and have image use control music
<img class="hvr-shrink square" alt="pause" src="media/gogh.png" onclick="document.getelementbyid('minuscolo').pause()">
i can assign play or pause function it, can't find way use both functions on same image if music playing acts pause button , if there's no music playing acts play button.
you can achieve want reading paused
property. return true
if audio paused, or false
if it's not. apply .play()
or .pause()
methods accordingly.
to this, use simple if ... else
structure (pseudocode):
if (audio.paused) { audio.play() } else { audio.pause() }
or make shorter use conditional ternary operator (? :
) (pseudocode):
audio.paused ? audio.play() : audio.pause()
that applied code (changed image , audio demo):
<audio class="topper" autoplay loop id="minuscolo"> <source src="http://www.html5tutorial.info/media/vincent.mp3" type="audio/mpeg"/> browser not support audio element. </audio> <img class="hvr-shrink square" alt="pause" src="http://lorempixel.com/g/100/100/abstract/" onclick="document.getelementbyid('minuscolo').paused ? document.getelementbyid('minuscolo').play() : document.getelementbyid('minuscolo').pause()">
you can see on jsfiddle.
having online bit ugly. nicer if attached click
event on element called function, , have javascript logic separated html code. like explained on w3c wiki link.
Comments
Post a Comment