
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to create Frame by Frame Animation using CSS and JavaScript?
Frame by frame animation is a technique used in animation to produce motion by displaying a series of static images which are sequentially displayed. The appearance of motion is achieved by displaying the images in rapid succession.
The followings are needed before we go to create the frame by frame animation−
A series of images (frames)
A web page with CSS and JavaScript
Approach
The process of creating frame by frame animation using CSS and JavaScript is relatively simple.
STEP 1 – First, you need to create a series of images (frames) that you want to be displayed in succession.
STEP 1 – Next, you need to create a web page with CSS and JavaScript that will load and display the images in rapid succession.
Full working code example
Here is a full working code example of how to create frame by frame animation using CSS and JavaScript. The code will load and display 2 images in succession.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Frame by Frame Animation</title> <style> #container { width: 400px; height: 400px; position: relative; } #container img { position: absolute; top: 0; left: 0; } </style> </head> <body> <div id="container"> <img src="https://www.tutorialspoint.com/static/images/logo-color.png" /> <img src="https://www.tutorialspoint.com/images/QAicon-black.png" /> </div> <script> var container = document.getElementById('container'); var images = container.getElementsByTagName('img'); var currentImage = 0; function changeImage() { images[currentImage].style.display = 'none'; currentImage = (currentImage + 1) % images.length; images[currentImage].style.display = 'block'; } setInterval(changeImage, 1000); </script> </body> </head>
HTML
The HTML code is very simple. It consists of a div element with an id of "container". Within the div element, there are 2 img elements. These img elements are the frames of the animation.
CSS
The CSS code styles the div element and the img elements. The div element is given a width and height. The img elements are positioned absolutely within the div element.
JavaScript
The JavaScript code is where the magic happens. First, the code gets a reference to the div element and the img elements. Next, the code defines a variable named "currentImage". This variable will be used to keep track of which image is currently being displayed.
The code then defines a function named "changeImage". This function will hide the current image and display the next image in the sequence.
Finally, the code uses the setInterval function to call the "changeImage" function every 1000 milliseconds (1 second). This will cause the images to be displayed in rapid succession, creating the illusion of motion.
And that's all there is to it! With just a few lines of code, you can create a simple CSS and JavaScript frame by frame animation.
- Related Articles
- Frame by frame animation in HTML5 with canvas
- How to create inline frame using HTML5
- How to create Candle Animation effect using CSS?
- How to create sliding text reveal animation using HTML and CSS?
- How to create button hover animation effect using CSS?
- How to create gooey balls animation using HTML & CSS?
- How to create Incoming Call Animation Effect using CSS?
- How to create Shooting Star Animation effect using CSS?
- How to create scatterplot using data frame columns in R?
- How to create scatterplot by standardizing the columns of a data frame using ggplot2 R?
- How to Create Typewriter Animation with CSS?
- How to create data frame using nested list elements in R?
- How to create bar plot using ggplot2 with structure data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- How to create a vector of data frame values by rows in R?
