JavaScript is disabled! Please enable JavaScript in your web browser!

Freestyle Academy of Communication Arts & Technology

1299 Bryant Ave, Mt. View, CA 94040 T 650-940-4650 x5090
2 Required Classes: English and Digital Media 3rd/Elective Class:  + Animation or Design or Film

Back to list of all examples

Useful Stuff About:

Implementing an SFX when clicking on a button

Click on a button to hear the effect.

Download the example SFXs here.

Create a .mp3 of your music (If not already an mp3 file, use Audio Converter to convert to an mp3) and name it btnClickSFX.mp3

Move the file into you project#/media folder

Here's the code that makes this all work:

Scripts needed (copy and paste inside <head>)


<script type="text/javascript" src="https://www.freestyleacademy.rocks/jquery/minified.js"></script><!-- only ONE of these on your html page -->
<script type="text/javascript" src="PATHTOYOUR/p#-scripts.js"></script><!-- ADJUST as needed -->

Here's the HTML code


<nav>
<div id="btn1" class="btn">Button 1</div>
<div id="btn2" class="btn">Button 2</div>
<div id="btn3" class="btn">Button 3</div>
<div id="btn4" class="btn">Button 4</div>
</nav>

Here's the CSS - obviously modify as needed


nav {
display:block;
height:50px;
width:800px;
margin:0 auto;
top:0px;
position:relative;
}
.btn {
width:200px;
height:50px;
float:left;
text-align:center;
cursor:pointer;
font-size:3em;
z-index:10;
}
.btn:hover {
color:#F00;
}

Here's the javascript code


var audioOnClick;
//initial audio settings
audioOnClick = document.createElement('audio');//sets variable as an audio type rather than text, number, array, etc.
audioOnClick.setAttribute("src", "project#/media/btnClickSFX.mp3");//locates the audio mp3 file
audioOnClick.load();//auto loads the audio so it's ready to play immediately, i.e. no downloading then playing delay
//actions for when a .btn is clicked
$(".btn").click(function(){
audioOnClick.play();//this actually plays the SFX
});