HTML5 Vibration API: Vibrator test, Examples, Plugins, and Code
Have you ever wanted to make your phone vibrate right from a website using JavaScript?
In this complete guide, you'll learn how to leverage the new HTML Vibration API to create immersive web experiences by adding vibration effects with just a few lines of JavaScript code.
Try out Phone Vibration Simulator tool to test the vibration patterns on your device.
Before implementing vibration effects, it's important to ensure your overall website SEO is optimized. Consider getting a professional SEO Audit to identify any critical issues.
Why Use the Vibration API on Websites?
Vibration effects can provide tactile feedback to enhance UX for mobile users. When used sparingly, vibrations can make web elements feel more interactive without being annoying or overused.
Some great uses cases:
- Provide haptic feedback on button clicks
- Notifications and alerts
- Fun effects paired with music or themes
With careful design, vibrations can significantly improve the UX on mobile sites and apps. ✨
Let's take a quick look at how it works:
In this complete guide you'll learn:
- Browser Support - Which browsers and devices support the Vibration API
- Using navigator.vibrate() - How to trigger vibrations with code
- Vibration Patterns - Creating complex vibration sequences
- Code Examples - Useful snippets, libraries and plugins
So if you want to make your web projects vibrate, buzz, shake, and rattle - let's get started!
While vibration effects can enhance UX, they should be part of a broader SEO strategy. SEO Audit offers comprehensive audits to help improve your overall search rankings and visibility
Vibration API Browser Support
The Vibration API is supported in most modern browsers, but there are some exceptions:
Device/Browser | Support |
---|---|
iOS Safari | ❌ No |
Opera Mini | ❌ No |
IE | ❌ No |
Android Browser | ✅ Since Android 4.4 |
Chrome | ✅ Yes! |
Firefox | ✅ Yes! |
Your Browser |
The vibration API is implemented in navigator.vibrate
. So calling the function makes your phone vibrate. You can test if your browser is recent enough to have the vibrate
function in navigator
. To check for support in JavaScript:
There are some key limitations:
- Requires actual vibration hardware in the device
- Page needs to be visible - doesn't work in background tabs
- Some browsers limit excessive vibration
Now let's look at how to actually trigger vibrations!
Usage of navigator.vibrate
The navigator.vibrate
function either accepts a number or an array of numbers.
In the following example the device will vibrate for 1000 milliseconds (ms):
Test Code
The vibration pattern is formed by milliseconds of duration of the vibration and the duration of the waiting period.
In this example the devices will vibrate for 1000 ms, wait 500 ms and vibrate again.
Test Code
Any new call stops the previous vibration sequence. If the page is no longer visible, like locking the device, minimizing the window, moving to another tab then the vibration also stops.
In this example the devices will stop vibrating.
Test Code
How to Vibrate your Phone on the Web
We can keep vibrating until the user stops touching the device. The vibration stops after a while though. But it's not meant to be pressed infinitely anyway.
In this example the device will keep vibrating until the touch event has stopped
Test Code
Vibrate on click
The best use case I can imagine for this API is for buttons. You get a little haptic feedback like you get for native apps. This can be done by setting the vibration to a very low number. For me 50ms seems ideal.
In this example all buttons and links on this page vibrate on tap. We also detect if the device is mobile and use touchend.
Test Code
Vibrate via checkboxes
Vibrating after clicking a sliding checkbox is also very helpful. It feels really natural. There is a short vibration at the start, then a period of waiting whilst the checkbox is moving and then a longer vibration at the end.
In this example, the checkbox will vibrate.
Vibration Duration
If you're not sure how long the haptic feedback should be. You can experiment with various timespans. Try all of these buttons out a mobile device. Anything above 100ms seems to long for me.
In this example the following buttons vibrate each on a different timespan.
Vibrate on notification
Another great use case are for notifications. These can be a bit longer than haptic feedback. Patterns can also be used to differentiate.
Please be aware of the vibration notification on the phone and try not to replicate them as to not to confuse the user. Some visual feedback together with the vibration would be ideal.
Click the buttons below. When the progress bar reaches the end. You get a notification! Each button is a different vibration pattern.
// Vibrate on completion
var pattern = [500, 100, 500];
$(".progress .bar")
.css({width: "0%"})
.animate({width: "100%"}, {
duration: 1000,
complete: function() {
if (canVibrate) navigator.vibrate(pattern);
}
});
Vibration with Song Patterns
Groggie mentioned his blog post on using the Vibration API for music and theme songs. It's a really cool example of what can be done using the Vibration API and some creative thought. Click on the titles below to play.
Vibration API Code Examples
Vue.js Vibration
I used to write my own Vibration API code in Vue.js, but with the hooks being introduced into Vue 3, I started using a hook called useVibrate from @vueuse/core.
Next.js and React vibration
In Next.js and React, you can use the Web Vibration API directly in functional components. Here's an example of how you can create a custom hook to handle vibrations:
You can then use this hook in your components to trigger vibrations based on user interactions or other events.
Otherwise, here's an example of how you can create a button that vibrates when clicked:
jQuery.vibrate.js
If doing it manually seems difficult for you, I've written a jquery plugin that can get you started right away.
Download and embed the code then initialize in one of the following ways:
I also made a slideshow: Learn About The Vibration API from Illyism. Feel free to share it and show it around.
Interested in more Javascript and CSS articles? Check out our article on CSS Text Gradients.
Ilias is a SEO entrepreneur and marketing agency owner at MagicSpace SEO, helping small businesses grow with SEO. With a decade of experience as a CTO and marketer, he offers SEO consulting and SEO services to clients worldwide.
Snyk is a popular tool for code security, but it's not the only option. Here are 10 alternatives to Snyk that you may want to consider.
Discover the top 7 AI girlfriend apps and websites for 2024. From free options to premium experiences, find your perfect virtual companion today.
Looking for perfect professional headshots? Compare the 10 best AI headshot generators and create stunning photos in minutes.
Danny Postma is a Dutch entrepreneur, developer, and AI innovator known for his successful "indie hacking" ventures and contributions to the online entrepreneur community. Learn about his journey from SEO specialist to AI product creator, and how he built a million-dollar business.