Ads 468x60px

Labels

Wednesday, May 9, 2012

Blink and bounce effect on image or object using jquery

Yesterday, Sushil asked me how can we bounce and blink an image or div using jQuery.In this post, I”ll show you how you can add bounce or blink effect to an image or div using jQuery.
Live Demo
First of all, let’s begin with the html code to add blink and bounce effect to your object using jQuery.
HTML Code:
<div id="object" class="message"> lorem ipsum dolor sit amet, consectetuer adipiscing elit orem ipsum dolor sit amet, consectetuer adipiscing elit </div>
Here the object which is needed to be animated is defined with the class”message” and with id “object”. Remember that, you can also put image in that “div” to bounce or blink.
Css Code:
.message{
border:1px solid #CCCCCC;
padding: 5px;
width: 150px;
background-color:#F4F4F8;
text-align:justify;
position:absolute;
top:50px;
}
You can see the attributes of the “message” class in css. You can change the above attributes as your need but remember one thing that the position property should be “absolute”. If you put it “relative”, the other object around the animated “div” or “image” starts get moved.
Javascript Code:
For blinking out the Object
//hide the object with blink effect
$("#blink_out").click(function()
{
  $("#object").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100);
});
As you can see above code, it is straightforward. When an element with id “blink_out” gets clicked, the element with id “object” gets animated with “fade out” and “fade in” effect of jQuery in alternative manner.
For blinking in the Object
//show the object with blink effect
$("#blink_in").click(function()
{
  $("#object").fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
});
As you can see this code is same as the above code but altered the way the “image” or “div” gets faded.
For bouncing the Object
//bounce an object
$("#bounce").click(function()
{
  $("#object").fadeIn(100).animate({top:"-=20px"},100).animate({top:"+=20px"},100).animate({top:"-=20px"},100)
.animate({top:"+=20px"},100).animate({top:"-=20px"},100).animate({top:"+=20px"},100);
});
In the above code, we’ve used jquery’s animate() function to bounce an “image” or “div”. The “top” attribute of the object is toggled to show the bouncing effect on the object.
Download full source code

0 comments:

Post a Comment

 

Sample text

Sample Text

Sample Text