Adding Sound to Your Website with Alpine.js
I've always believed the web should be a fun place to explore — a place full of surprise and wonder. Too much of the internet has been sterilized, flattened, and neutered. It makes me sad.
If you're reading this article on my website you have probably encountered my sound effects (like hover the nav and clicking my head). If you wonder why, I reply with why not?. Anything you can do to bring character and life back to the web is a good thing in my book.
The "How To" Part
I kept the backstory to 2 short paragraphs. I'll just assume you want to do this since you're here.
I love using Alpine.js and have found it to be one of the simplest ways to add sound effects to my sites, no matter how its built. Here's what you do.
- Include Alpine.js
- Initialize it globally
- Add a
refattribute to an
- Add an "on" event handler to
play()the audio file.
- That's it.
Here's a CodePen where you can see it in action.
You can play sounds on other interactions too.
On Input Focus
On Pressing Escape
Go have fun. Make something weird. Make someone laugh.
Published Aug 29, 2020