Hover audio file (and stop playback when output)

What is the best solution for playing an audio file with your mouse via JavaScript? And stop it when the mouse leaves the link. JQuery is available.

<a href="/test.mp3" class="play">play</a>
+3
source share
3 answers
<audio id="mysound" src="mysound.wav"></audio>
<button onmouseover="document.getElementById('mysound').play()" onmouseout="document.getElementById('mysound').pause()">Hover me!</button>
+3
source
+1
source
<script type="text/javascript">
    (function($) {
        $(function(){
                $("a.play").each(function() {
                  $(this).mouseover(function(){ 
                       var mp3file = $(this).attr('href');
                       // asign this mp3file to the player and play it
                  }).mouseout(function() { 
                       // tell the mp3 player to stop
                  });
                });
        });
    })(jQuery);
</script>

you can use the player that unomi suggested. works great

+1
source

Source: https://habr.com/ru/post/1749381/


All Articles