w3resource

jQuery: Animate the paragraph element with a number of different properties

jQuery Fundamental - I : Exercise-19

Using jQuery click the button to animate the paragraph element with a number of different properties.

<body>
<p id="pid">jQuery</p>  
<button id="button1">Click to see the animation</button> 
</body>
p {
    background-color: #B0E0E6;
    width: 70px;
    border: 1px solid red;
  }
Animation properties :
    width: "70%",
    opacity: 0.4,
    marginLeft: "0.6in",
    fontSize: "3em",
    borderWidth: "10px"

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Click the button to animate the paragraph element with a number of different properties.</title>
</head>
<body>
<p id="pid">jQuery</p>
<button id="button1">Click to see the animation</button>
</body>
</html>

CSS Code:

button {
    display: block;
    margin: 20px 0 0 0;
    }  

p {
    background-color: #B0E0E6;
    width: 70px;
    border: 1px solid red;
  }

JavaScript Code :

$('#button1').click(function(){ 
$( "#pid" ).animate({
    width: "70%",
    opacity: 0.4,
    marginLeft: "0.6in",
    fontSize: "3em",
    borderWidth: "10px"
  }, 1500 );
});

Used Methods :

  • .animate(): Perform a custom animation of a set of CSS properties.

See the Pen jquery-fundamental-exercise-19 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Using jQuery count all elements within a division.
Next: Using jQuery animates a div's left property with a relative value.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/jquery-exercises/1/jquery-fundamental-exercise-19.php