Tag Archives: Javascript

jQuery Animation

The .animate() method allows us to create animation effects on any numeric CSS property. This method changes an element from one state to another with CSS styles. The CSS property value is changed gradually, to create an animated effect.

Syntax is:

(selector).animate({styles},speed,easing,callback)

  • styles: Specifies one or more CSS properties/values toanimate.
  • duration: Optional. Specifies the speed of the animation.
  • easing: Optional. Specifies the speed of the element in different points of the animation. Default value is “swing”.
  • callback: Optional. A function to be executed after the animation completes.

Simple use of animate function is,

Hide Copy Code
$(“btnClick”).click(function(){
$(“#dvBox”).animate({height:”100px”});
});

jQuery eq() And get()

eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.

get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can’t be used.

jQuery $(this) And ‘this’

this and $(this) refers to the same element. The only difference is the way they are used. ‘this’ is used in traditional sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.

$(document).ready(function(){
$(‘#spnValue’).mouseover(function(){
alert($(this).text());
});
});
In below example, this is an object but since it is not wrapped in $(), we can’t use jQuery method and use the native JavaScript to get the value of span element.

$(document).ready(function(){
$(‘#spnValue’).mouseover(function(){
alert(this.innerText);
});
});