Hello! I finally got a music button working and wanted to share the code!!! I have this in a container on top of an image, so if you want it separate, you might have to tinker with it some.
You'll add this inside your STYLE tag:
.music-button {
position: absolute;
top: 50px; /* Adjust this number to move the button up or down! */
left: 50px; /* Adjust this number to move the button left or right! */
background: none;
border: none;
cursor: pointer;
z-index: 10; /* Ensures the button stays on top of the image! */
In your BODY tag above the container, you'll add this. It's your script to make the music play:
<audio id="myMusic" src="MUSIC.MP3"></audio>
<script>
function toggleMusic() {
var audio = document.getElementById("myMusic");
var img = document.getElementById("buttonImage");
if (audio.paused) {
audio.play();
img.src = "YOUR_PAUSE_BUTTON.PNG";
} else {
audio.pause();
img.src = "YOUR_PLAY_BUTTON.PNG";
}
}
</script>
Beneath this, add your container! Beneath the container code, you'll add this:
<button onclick="toggleMusic()" class="music-button">
<img id="buttonImage" src="YOUR_MUSIC_BUTTON.PNG" alt="Play Music" width="100">
</button>
All in all, it should look like this! This image, of course, contains my own chosen song + the assets I created!
