开发者

jQuery: stop audio on mouseleave

开发者 https://www.devze.com 2023-04-13 00:25 出处:网络
I\'m using the following script to play a sound when someone hovers a div. $(document).ready(function(){

I'm using the following script to play a sound when someone hovers a div.

$(document).ready(function(){
 var birdaudio = birdhover.find('a开发者_Python百科udio')[0];    

 $('.sound').mouseenter(function(){ birdaudio.play(); });
 $('.sound').mouseleave(function(){ birdaudio.stop(); });
});

It actually works, but the audio keeps on playing till the end of the sound, even when not hovering anymore. Is there something I could to do to prevent that? I wan't that the audio stops whenever you 'mouseleave' the div.

Hoping for help!


There is no .stop() for HTML5 media elements. You must call pause() on the element instead.

In order to fully replicate the functionality of a stop (both stop the audio and set it's playback position to the very beginning), you would then follow your pause() call with birdaudio.currentTime = 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号