Vibration API

Smart Phone by Stefan Bumbeck from The Noun Project javascript guide  •  14/02/2014

The Vibration API1 is an API specifically made for mobile devices as they are thankfully the only devices that have a vibrate function. The API allows developers to vibrate a device (in a pattern) for a given duration.

  • Note: This article is best viewed on a mobile device. Go to on your mobile device to quickly go to this article.

Does my Device Support the API?

  • 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.

Mozilla had their own implementation mozVibrate so some browsers may support that instead.

var canVibrate = "vibrate" in navigator || "mozVibrate" in navigator;

if (canVibrate && !("vibrate" in navigator))
    navigator.vibrate = navigator.mozVibrate;

However, this doesn't mean that your device can vibrate. Just that it's recent enough. There are a few requirements you need to meet.

  1. You need the hardware for it.
  2. The page needs to be visible.
  3. Browser-specific implementation prevents the vibration.


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

// vibrate for 1000 ms

// or alternatively

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

// device will     vibrate    wait    vibrate
navigator.vibrate([1000,      500,    1000]);

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

// Stop vibrating

Start / Stop

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

var isMobile = (/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent);

$(".button").on(isMobile ? 'touchstart' : 'mousedown', function(e) {
    navigator.vibrate(Infinity); // Infinity is a number

$(".button").on(isMobile ? 'touchend' : 'mouseup', function(e) {


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

var isMobile = (/iPhone|iPod|iPad|Android|BlackBerry/).test(navigator.userAgent);

$(".button, a").on(isMobile ? 'touchend' : 'click', function(e) {

Tap Tap Tap Tap


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.

<div class="ui slider checkbox">
    <input id="product-1" type="checkbox">
    <label for="product-1">Duvel</label>
<div class="ui slider checkbox">
    <input id="product-2" type="checkbox">
    <label for="product-2">Jupiler</label>
$(".ui.checkbox").click(function() {
    navigator.vibrate([5, 200, 20]);

How Long is Long Enough?

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.

10 20 30 40 50 75 100 150 200 500 1000 2000


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.

Go! Go! Go! Go!

// 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);


If doing it manually seems to hard for you, I've written a jquery plugin that can get you started right away.


Github Download Documentation

Download and embed the code then initialize in one of the following ways.

// Vibration for 50ms on all .button on click

// Vibrate for 20ms on click

// Vibrate for 50ms on click

// Vibrate for 100ms on click

// Vibrate for 1000ms on touchstart. Stop vibrating on touchend.
    duration: 1000,
    trigger: "touchstart"

Song Patterns

Groggie mentioned his blog post2 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.


more on the Gearside website

Here are some other links to get you some inspiration:

I also made a slideshow: Learn About The Vibration API from Ilias Ismanalijev. Feel free to share it and show it around.

  1. For more documentation visit the W3C document on vibration: 

  2. Custom vibration patterns for mobile devices | Gearside 

The two most important days in your life are the day you are born and the day you find out why.

Mark Twain