alternate sound effects on anchor click
I'm messing with HTML5 and audio tags for a little开发者_运维技巧 project I've been working on. Basically, I have an anchor tag that activates a jquery slideToggle. I want to add sound effects to it so that clicking the anchor plays one sound effect when it opens and a different sound effect when it closes. I've spent hours trying to figure out how, but my Javascript and JQuery knowledge is extremely limited. I'm trying to avoid using external players or Flash.
I finally got it working. I used the code below. I included currentTime and Pause in order to prevent the sounds from overlapping and so that they play from the beginning every time.
var toggle=true;
function play_effect1(){
if(toggle){
document.getElementById('close').currentTime = 0;
document.getElementById('close').pause();
document.getElementById('open').play();
toggle=false;
}
else {
document.getElementById('open').currentTime = 0;
document.getElementById('open').pause();
document.getElementById('close').play();
toggle=true;
}
}
精彩评论