Fade In/Fade Out Effect using jQuery
Refer to this post to know more about Fade In/Fade Out effect in jQuery.
Hide/Show and Toggle HTML component/DIV/SPAN using jQuery
Today most of the web pages have sections which has a hide/show icon that toggle the visibility of that section. Also sometimes you may want to hide/show specific html component like button, textbox etc or div/span on some events. This can be easily achieved through jQuery.
Suppose we have a textbox and a button and we need to hide the textbox on click of the button. Following is the code snippet to do this.
In above snippet we are selecting the textbox using its id #user and then calling hide() function to hide it. Simple isn’t it?
Similarly to show any component we can use following code:
Also, not only you can hide/show any html component but also you can specify speed and also a callback function that gets called once the content is hidden or shown.
Following is the syntax of hide() and show() methods with speed:
hide(speed) and show(speed)
speed can be “slow”, “normal” and “fast”. You can also provide number as an argument which represents milliseconds.
Also you can specify a handler function that gets called once the animation is completed.
Also you can use slideToggle() function to achieve toggle functionality to do sliding up and down. slideToggle() also comes with the same arguments as slideDown/slideUp.
Animate html component using jQuery
jQuery provides an animate() function that can be used create your own custom animations.
animate(params, speed, easing, callback);
The key aspect of this function is the object of style properties that will be animated, and to what end. Each key within the object represents a style property that will also be animated (for example: “height”, “top”, or “opacity”).
The value associated with the key represents to what end the property will be animated. If a number is provided as the value, then the style property will be transitioned from its current state to that new number. Oterwise if the string “hide”, “show”, or “toggle” is provided, a default animation will be constructed for that property.
params (Hash): A set of style attributes that you wish to animate, and to what end.
speed (String|Number): (optional) A string representing one of the three predefined speeds (“slow”, “normal”, or “fast”) or the number of milliseconds to run the animation (e.g. 1000).
easing (String): (optional) The name of the easing effect that you want to use (Plugin Required).
callback (Function): (optional) A function to be executed whenever the animation completes.
An example of using an ‘easing’ function to provide a different style of animation. This will only work if you have a plugin that provides this easing function (Only ‘linear’ is provided by default, with jQuery).