2016-08-19 271 views
1
var audio = new Audio('click.mp3'); 
$("nav li").mouseover(function() { 
    audio.play(); 
}).mouseout(function() { 
    audio.pause(); 
}); 

這裏是我的代碼,懸停的音頻文件播放很多次,我想知道如何添加,使其懸停很多時候不是隻播放一次HTML5音頻播放多次

+0

魔法門傳播https://api.jquery.com/event.stoppropagation/ – mattfetz

+0

您可以使用mouseenter代替鼠標懸停 –

回答

1
var audio = new Audio('click.mp3'); 
var hasBeenPlayed = false; 
$("nav li").mouseover(function() { 
    if(!hasBeenPlayed) { 
    audio.play(); 
    hasBeenPlayed = true; 
} 
}).mouseout(function() { 
    audio.pause(); 
    hasBeenPlayed = false; 
});