Animation in JavaScript
You can use JavaScript to create a complex, unlimited effect, with the following elements:
- Fireworks
- Fade Effect
- Roll-in or Roll-out
- Page-in or Page-out
- Object movements
You should consider the existing effects library built on JavaScript: Script.Aculo.us .
This tutorial provides you with a basic understanding of how to use JavaScript to create an effect.
JavaScript can be used to move several DOM elements ( ,
JavaScript provides the following two functions that are used frequently in animation programs.
- setTimeout (function, duration) - This function calls the Function after the duration (in milliseconds) from now.
- setInterval (function, duration) - This function calls the function after each duration (in milliseconds).
- clearTimeout (setTimeout_variable) - This function removes any timer set by the setTimeout () function.
JavaScript can also set some properties of a DOM object including its position on the screen. You can set the top and left properties of an object to determine its position anywhere on the screen. Here is the syntax:
// Set distance from left edge of the screen. object . style . left = distance in pixels or points ; or // Set distance from top edge of the screen. object . style . top = distance in pixels or points ;
Hand operation animation
We now implement a simple animation using DOM object attributes and JavaScript functions as follows. The following lists the various DOM methods:
We are using the getElementById () function to get a DOM object and then assign it to a Global variable, imgObj .
We have defined an initialization function init () to initialize imgObj , where we have set its position and left properties.
We are calling the initialization function at the time of loading the window.
Finally, we are calling moveRight () function to increase the left distance by 10 pixels. You can also set it to a negative value to move it to the left.
For example
Try the following example:
JavaScript Animation
Click button below to move the image to right type = "button" value = "Click Me" onclick = " moveRight (); " />
Result
Run the above command to see the result
Automation animation
In the above example, we saw how an image moves right every time you click. We can automate this process using the setTimeout () function as follows:
Below, we add more methods. That is:
The moveRight () function is calling the setTimeout () function to set the position of imgObj.
We added a new function stop () to delete the timer set by the setTimeout () function and to set the object at the initialization position.
For example
You try the following code:
JavaScript Animation
Click the buttons below to handle animation type = "button" value = "Start" onclick = " moveRight (); " /> type = "button" value = "Stop" onclick = " stop (); " />
Result
Run the above command to see the result
Rollover with a Mouse Event
Below is a simple example to illustrate rollover images with a Mouse Event.
Let's look at what is being used in this example:
At the time of loading this page, the if command checks for the existence of the Image object. If this Image object is not available, this block will not be executed.
Image () constructor creates and reloads a new Image object called image1 .
The src attribute is assigned the name of the external image file called ./images/html.gif.
Similarly, we create the image2 object and assign ./images/http.gif in this object.
The # disables the link so that the browser doesn't try to reach the URL when clicked. This link is an image.
The onMouseOver error handling method is activated when the user moves the mouse over the link (the image), and onMouseOut event handler is triggered when the user moves the mouse off the link.
When moving the mouse over the image, the HTTP image changes from the first image to the second image. When moving the mouse out of the image, the original image is displayed.
When moving the mouse off the link, the first initialization image html.gif will reappear on the screen.
Rollover with a Mouse Events
Move your mouse over the image to see the result href = "#" onMouseOver = " document . myImage . src = image2 . src ; " onMouseOut = " document . myImage . src = image1 . src ; " >name = "myImage" src = "./images/html.gif" />
Result
Run the above command to see the result
According to Tutorialspoint
Previous article: Form Validation in JavaScript
Next lesson: Multimedia (Multimedia) in JavaScript
You should read it
- Instructions to turn off Focus Ring Animation on Mac
- How to use Face Animation to convert still portraits to GIFs
- 7 best computer animation software
- Disney's AI model creates animated sequences from scripts
- Tips and tricks for using Animation in CSS that you need to know
- How to Make Your Own Animation
- Movement turns static image into animation, have you tried it?
- Top 5 most professional animation software in 2020
May be interested
- Learn about ES6 in Javascriptes6 refers to version 6 of the ecma script programming language. ecma script is the standard name for javascript and version 6 is the next version after version 5, released in 2011.
- Summary of JavaScript exercises with sample codein order to make your javascript learning easier, tipsmake.com has compiled a number of javascript exercises with sample solutions for you to practice.
- JavaScript location in HTML Filethere is flexibility in providing javascript code anywhere in an html document. however, the most preferred ways to include javascript in an html file.
- How to Turn on JavaScriptjavascript is a language used in many websites to provide additional functionality and access to the user. developers use javascript to deliver a large part of their site's functionality to you, the user. your browser must have javascript...
- What is the difference between Java and JavaScript?although the names of java and javascript seem to be related (javascript seems to be a script in java?), but that's the only similarity. these two languages are not technically related to each other.
- Void keywords in JavaScriptvoid is an important keyword in javascript that can be used as a unary operator before its single operand, which can be in any type. this operator defines an expression to be evaluated without returning a value.
- JavaScript code to generate error charts & graphsthe example below illustrates a sample variance chart created with javascript that incorporates a column chart. you will also get the source code for reference and editing as you wish.
- Introduction to 2D Array - 2-dimensional array in JavaScriptin the following article, we will introduce and show you some basic operations to create and access 2-dimensional arrays - 2d array in javascript. essentially, a 2-dimensional array is a concept of a matrix of matrices - a matrix, used to store information. each 1 element contains 2 separate indexes: row (y) - column and column (x) - column.
- Operator in JavaScriptwe see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
- How to Disable JavaScriptthis wikihow teaches you how to turn off your web browser's javascript support. javascript is responsible for loading dynamic content in webpages, so turning it off can help websites load faster than usual. most web browsers and their...