views:

137

answers:

2

What is the best solution to play an audio file on mouse over via JavaScript? And stop it when the mouse leaves the link. jQuery is available.

  <a href="/test.mp3" class="play">play</a>
A: 

http://www.schillmania.com/projects/soundmanager2/
Provides HTML5 + legacy support

unomi
SoundManager is a little heavy. No other suggestions?
powtac
A: 
<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

TheBrain
2 Things here, there's a `.hover()` function you should check out, and there's never any need for `.each()` when binding, just call `$("a.play").click()` for example, it'll bind each of the matches for that selector :)
Nick Craver
Thanks Nick, I forgot about hover!
powtac