Enable

This is a pausable animation.

You can pause this, as well as all of the animated content on this page, with the control at the top of the screen

The planet saturn in front of an animated starfield

Pause Animation Controls

This can be used for new builds or existing work with animations. You should always test the animations you are using to see if it works with this component. If not, please look at the detailed notes below on how to have your animations support this component.
This solution described below is available as an NPM module. (Module installation instructions)

Pause all the CSS, Canvas, Video, SVG SMIL and GIF animations on this page with the checkbox at the top of this page. The CSS, Video and SVG SMIL animations don't know anything about the checkbox. The Canvas and GIF animations only require a small amount of set up to work.

(Note: if you have set your operating system to Reduce Motion, it will be checked by default and animations on the page will not play until the checkbox is unchecked)

Quick Start guide

Just download the script and include it at the end of the body:

☜ Scroll to read full source ☞

                    
                

After that, just include it in the bottom of your page. The control to pause the animations must be structured the following way:

☜ Scroll to read full source ☞

                    
                

For animated GIFs or WEBP, you will have to structure your HTML a certain way (see the animated GIF example below for details).

How Does It Work

The TL;DR:

Code walkthroughs

This section will show how the script stops the different type of animations.

CSS Animations

The animation of Saturn from one of my previous blog posts that appears above is animated via CSS.

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

Pausing requestAnimationFrame Animations

Most Canvas Animations (like this great elastic collision demo from Ana Tudor) rely on code using requestAnimationFrame() to produces a frame of animation. When the checkbox is checked, we just set window.requestAnimationFrame to a dummy function, and set it back when the checkbox is unchecked. The animation below wasn't made with pausing in mind ... my pause script "just made it work".

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

SMIL Animation Demo

The animation below (made by the talented Lenka from their CodePen of this demo) uses SMIL to animate the SVG. Note that although there was a lot of noise about SMIL being deprecated in Chrome, this deprecation was suspended (i.e. it is still a web standard and will continue to be in the forseeable future, due to pushback from the web development community).

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

Animated GIF/WEBP Example

Unlike SVG SMIL animations, there is no browser API to pause GIF/WEBP animations. This script works around this by allowing the developer to include two different renditions of the image: one animated, the other not. CSS is then used to hide and show each rendition according the user's preference.

(If you are looking for a way to add pause buttons on GIF animations, please take a look at the animated gif pause button examples).

Animated drawing of a man running A drawing of a man running

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

Vanilla HTML5 Video

Videos embedded by the video tag can be paused automatically by the script as well. Note that when the user unchecks the "Pause animations" checkbox, the videos will only start playing if they were playing when the checkbox was originally checked.

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

AblePlayer

Since AblePlayer plays videos Note that when the user unchecks the "Pause animations" checkbox, the videos will only start playing if they were playing when the checkbox was originally checked. (Since this video does not autoplay, you must play it first and then check the "Pause Animations" checkbox in order to test it properly)

Example code explanation

Below is the HTML of the above example. Use the dropdown to highlight each of the individual steps that makes the example accessible.

☜ Scroll to read full source ☞

                    
                

Making scrollIntoView() Respect the Pause Control

It is possible to make the a DOM element's scrollIntoView() method respect the setting of the pause control. You will notice that all the code walkthroughs will not animate when changing steps and the "Pause animations" control is checked. This part of the code shows you how this works:

☜ Scroll to read full source ☞

                    
                

Installation Instructions

You can load this JavaScript library into your application in serveral ways:

If you haven't done so already, choosing which you should use is obviously a major architectural decision. Here are a few articles that will help you decide:

Important Note On The CSS Classes Used In This Module:

This module requires specific CSS class names to be used in order it to work correctly. These CSS classes begin with pause-anim-control__. Please see the documentation above to see where these CSS classes are inserted.

Using NPM/Webpack to load ES6 Modules:

  1. Install the enable-a11y NPM project. Developers who are using webpack should know how to configure webpack to import the enable modules correctly.
  2. You can use the module like this:
    ☜ Scroll to read full source ☞
    // import the JS module import pauseAnimControl from '~enable-a11y/js/modules/pause-anim-control'; // import the CSS for the module import '~enable-a11y/css/pause-anim-control'; // How to initialize the pauseAnimControl library pauseAnimControl.init();
  3. Alternatively, if you are using LESS you can include the styles in your project's CSS using:
    ☜ Scroll to read full source ☞
    @import '~enable-a11y/css/pause-anim-control';
    (If you are using it in your CSS, you will have to add the .css suffix)

Using ES6 modules natively.

This is the method that this page you are reading now loads the scripts.

  1. Grab the source by either using NPM, grabbing a ZIP file or cloning the enable source code from github.
  2. If you want to load the module as a native ES6 module, copy js/modules/pause-anim-control.js, and css/pause-anim-control.css from the repo and put them in the appropriate directories in your project (all JS files must be in the same directory).
  3. Load the CSS in the head of you document:
    ☜ Scroll to read full source ☞
    <html> <head> ... <link rel="stylesheet" href="path-to/css/pause-anim-control.css" > ... </head> <body> ... </body> </html>
  4. Load your scripts using:
    ☜ Scroll to read full source ☞
    <script type="module"> import pauseAnimControl from "path-to/pause-anim-control.js" pauseAnimControl.init(); </script>

Using ES4

Just do the same as the ES6 method, except you should get the JavaScript files from the js/modules/es4 directory instead of the js/modules/:
☜ Scroll to read full source ☞
<script src="path-to/es4/pause-anim-control.js"></script>