Functions - JavaScript Animation
Posted by Superadmin on May 07 2023 06:18:16

JavaScript Animation

By Priya PedamkarPriya Pedamkar
  

JavaScript Animation

Introduction to JavaScript Animation

JavaScript animation is used to make the object changed from one state to other state or moved from one position to other position by providing some special functionality through the code. This animation can be fade effect, roll-in or roll-out, page-in or page-out, object movements By default JavaScript provides default animation library that is Script.Aculo.us. Animation can also be used to move the HTML elements like <img>, <div>, <p>, <footer> etc. around the page according to the provided functionality to the code.

Advantages

How does Animation work in JavaScript?

Animation worked based on functions provided to animate the object. You can consider below frequently used functions for animation.

JavaScript also used for set the number of attributes of any element and also includes its position on the screen. We can also set the top and the left attribute of an object to position this object on anywhere on the screen.

Syntax:

var var= setInterval(getMyFun, 5);
function getMyFun() {
if (condition) {
clearInterval(var);
} else {
//required logic
}
}
Note: We can also write our own custom functionality for our own animation.

Examples

Below are the examples:

Example #1 – Moving the Image Right

Code:

<html>
<head>
<title>JavaScript Animation</title>
<!-- CSS Styles -->
<style>
img {
width: 250px;
height: 250px;
}
p {
color: green;
border: solid 3px red;
font-size: 28px;
}
h1, h2 {
color: brown;
text-align: center;
}
.button {
text-align: center;
}
input {
color: white;
background-color: blue;
font-weight: bold;
}
</style>
<!-- JavaScript logic-->
<script type="text/javascript">
var imageRef = null;
function start() {//start function to initialize the objects
imageRef = document.getElementById('myID');//read the images by ID
imageRef.style.position = 'relative';//set the image positon to relative
imageRef.style.left = '0px';//set image left postion 0px.
}
function moveImageRight() {//function for move the image right
imageRef.style.left = parseInt(imageRef.style.left) + 50 + 'px';
}
window.onload = start;//calling the start method while loading the script
</script>
</head>
<body>
<h1>Introduction to JavaScript Animation</h1>
<h2>Moving the Image Right hand side by clicking the button</h2>
<p>JavaScript animation is used to make the object changed from one
state to other state or moved from one position to other position by
providing some special functionality through the code. This animation
can be fade effect, roll-in or roll-out, page-in or page-out, object
movements etc. By default JavaScript provides default animation
library that is Script.Aculo.us. JavaScript animation can also be used
to move the HTML elements like img, div , p, footer tags etc. around
the page according to the provided functionality to the code.</p>
<form>
<img id="myID" src="1.jpg" />
<div class="button">
<input type="button" value="Move Right" onclick="moveImageRight();" />
</div>
</form>
</body>
</html>

Output:

javascript animation output 1

After Clicking Move Right button:

javascript animation output 2

javascript animation output 3

Example #2 – Moving the content left

Code:

<html>
<head>
<title>JavaScript Animation</title>
<!-- CSS Styles -->
<style>
p {
color: blue;
border: solid 4px green;
font-size: 28px;
}
h1, h2 {
color: red;
text-align: center;
}
.button {
text-align: center;
}
input {
color: white;
background-color: maroon;
font-weight: bold;
width:120px;
font-size:20px;
height:40px;
}
</style>
<!-- JavaScript logic-->
<script type="text/javascript">
var imageRef = null;
function start() {//start function to initialize the objects
imageRef = document.getElementById('myID');//read the images by ID
imageRef.style.position = 'relative';//set the contetn positon to relative
imageRef.style.right = '0px';//set content right postion 0px.
}
function moveImageLeft() {//function for move the image right
imageRef.style.right = parseInt(imageRef.style.right) + 50 + 'px';
}
window.onload = start;//calling the start method while loading the script
</script>
</head>
<body>
<h1>Introduction to JavaScript Animation</h1>
<h2>Moving the Content Right hand side by clicking the button</h2>
<form>
<p id="myID">JavaScript animation is used to make the object
changed from one state to other state or moved from one position to
other position by providing some special functionality through the
code. This animation can be fade effect, roll-in or roll-out, page-in
or page-out, object movements etc. By default JavaScript provides
default animation library that is Script.Aculo.us. JavaScript
animation can also be used to move the HTML elements like img, div ,
p, footer tags etc. around the page according to the provided
functionality to the code.</p>
<div class="button">
<input type="button" value="Move Left" onclick="moveImageLeft();" />
</div>
</form>
</body>
</html>

Output:

javascript animation output 4

After Clicking the Move Left Button:

output 5

output 6

Example #3 – Mouse hover change Image

Code:

<html>
<head>
<title>JavaScript Animation</title>
<style>
p {
color: maroon;
border: solid 4px blue;
font-size: 28px;
}
h1, h2 {
color: orange;
text-align: center;
}
</style>
<script type="text/javascript">
if (document.images) {//checking the document mages
var firstImage = new Image();//loading first image
firstImage.src = "n1.jpg";//first image url
var secondImage = new Image(); //loading second image
secondImage.src = "n2.jpg";//second image url
}
</script>
</head>
<body>
<h1>Introduction to JavaScript Animation</h1>
<p>JavaScript animation is used to make the object
changed from one state to other state or moved from one position to
other position by providing some special functionality through the
code. This animation can be fade effect, roll-in or roll-out, page-in
or page-out, object movements etc. By default JavaScript provides
default animation library that is Script.Aculo.us. JavaScript
animation can also be used to move the HTML elements like img, div ,
p, footer tags etc. around the page according to the provided
functionality to the code.</p>
<h2>Hover Cursor on to the image to see the other image</h2>
<a href="#" onMouseOver="document.myImage.src = secondImage.src;"
onMouseOut="document.myImage.src = firstImage.src;"> <img
name="myImage" src="n1.jpg" />
</a>
</body>
</html>

Output:

output 7

After hovering the cursor on to the Image

output 8

Conclusion

JavaScript animation is used to make the object movable or change its style. Animation can work with paragraph, header, images, footer or any html element tags. We can also apply time intervals to the application.