Overview
jQuery can be used to hide, show, fade in/out, or slide up/down HTML elements on the page, creating effects and animations with a great deal of control.
The following are some quick examples of the methods used to create effects. A complete list of effects can be found here along with more detailed descriptions of their intended use and syntax.
animate() Method
The animate() method performs a custom animation on an HTML element, changing one CSS property value to another gradually, creating an animated effect.
// animate the opacity of the element to 0.2, slowly, using "swing" easing
$('button').click(function() {
$('div').animate({opacity: '0.2'}, 'slow', 'swing');
});
fadeIn() Method
The fadeIn() method gradually changes, fades in, the opacity for the selected element from completely hidden to completely visible. The visible item will now take up space on the page, affecting the layout.
// fade element in over 2000ms using "swing" easing
$('button').click(function() {
$('div').fadeIn(2000, 'swing');
});
fadeOut() Method
The fadeOut() method gradually changes, fades out, the opacity for the selected element from completely visible to completely hidden. The hidden item will no longer take up space on the page, affecting the layout.
// fade element out over 2000ms using "swing" easing
$('button').click(function() {
$('div').fadeOut(2000, 'swing');
});
hide() Method
The hide() method hides the selected element. The hidden item will no longer take up space on the page, affecting the layout.
// hide the element over 2000ms using "swing" easing
$('button').click(function() {
$('div').hide(2000, 'swing');
});
show() Method
The show() method shows the selected hidden element. The shown item will now take up space on the page, affecting the layout. It works on elements hidden with jQuery methods and those with display: none in CSS.
// show the element over 2000ms using "swing" easing
$('button').click(function() {
$('div').show(2000, 'swing');
});
slideDown() Method
The slideDown() method slides down and shows the selected element. The visible item will now take up space on the page, affecting the layout. It works on elements hidden with jQuery methods and those with display: none in CSS.
// slide the element down over 2000ms using "swing" easing
$('button').click(function() {
$('div').slideDown(2000, 'swing');
});
slideUp() Method
The slideUp() method slides up and hides the selected element. The hidden item will no longer take up space on the page, affecting the layout.
// slide the element up over 2000ms using "swing" easing
$('button').click(function() {
$('div').slideUp(2000, 'swing');
});
jQuery Notes:
- To use jQuery on your site, it must first be downloaded from the official jQuery site and linked to in your document <head>, or linked to via a CDN in your document <head>
- It is generally good practice to place your jQuery code/function inside the document load function so that the action takes place ONLY after the document has finished loading
- When using jQuery, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.