Effects and animation
Effects and animation
1.2 .toggle()
javascript
Copy code
// Toggle visibility $('#myDiv').toggle();
You can also pass a boolean argument to .toggle() to explicitly show or hide an element:
javascript
Copy code
$('#myDiv').toggle(true); // Show the element $('#myDiv').toggle(false); // Hide the element
2. Fading Effects
These effects change the opacity of an element to create fade-in or fade-out transitions. They allow you to animate the opacity
from 0 (invisible) to 1 (fully visible) or vice versa.
2.1 .fadeIn()
javascript
Copy code
$('#myDiv').fadeIn(1000); // Fades in over 1 second (1000 ms)
2.2 .fadeOut()
javascript
Copy code
$('#myDiv').fadeOut(1000); // Fades out over 1 second
2.3 .fadeTo()
The .fadeTo() method changes the opacity to a specified level, over a certain duration.
javascript
Copy code
$('#myDiv').fadeTo(1000, 0.5); // Fade to 50% opacity over 1 second
2.4 .fadeToggle()
javascript
Copy code
$('#myDiv').fadeToggle(1000); // Fades in/out over 1 second
3. Sliding Effects
Sliding effects animate the height of an element, either increasing it to reveal content (.slideDown()) or decreasing it to hide
content (.slideUp()).
3.1 .slideUp()
3.2 .slideDown()
javascript
Copy code
$('#myDiv').slideDown(1000); // Slide down over 1 second
3.3 .slideToggle()
javascript
Copy code
$('#myDiv').slideToggle(1000); // Slide up/down over 1 second
4. Custom Animations
For more advanced effects, you can animate multiple CSS properties using the .animate() method. This method allows you to
animate any CSS properties that support numeric values, such as width, height, left, top, opacity, etc.
The .animate() method lets you animate multiple properties in a single call.
javascript
Copy code
$('#myDiv').animate({ width: '200px', // Animate width to 200px height: '150px', // Animate
height to 150px opacity: 0.5 // Fade the element to 50% opacity }, 1000); // Duration of the
animation (1000 ms)
jQuery supports different easing functions to control the animation speed curve. Common easing functions are linear, swing, and
custom functions.
javascript
Copy code
$('#myDiv').animate({ left: '100px' }, 1000, 'swing'); // 'swing' makes the animation start
slowly, then accelerate
javascript
Copy code
$('#myDiv') .animate({ left: '100px' }, 500) .animate({ top: '100px' }, 500) .fadeOut(500); //
Perform multiple animations in sequence
The .queue() method adds functions to a queue, so they are executed in order.
javascript
Copy code
$('#myDiv') .animate({ left: '100px' }, 500) .queue(function(next) { console.log('Animation
complete!'); next(); // Move to the next function in the queue }) .fadeOut(500);
The .delay() method delays the start of an animation for a specified time.
javascript
Copy code
$('#myDiv') .fadeIn(500) .delay(1000) // Wait 1 second before starting the next
animation .fadeOut(500);
6. Callback Functions
You can pass a callback function to be executed once the animation is complete.
javascript
Copy code
$('#myDiv').fadeIn(1000, function() { alert('Fade in complete!'); });
The callback function runs once the animation completes.
7. Animating Properties
In addition to animating properties like width, height, and opacity, you can animate positions (top, left) or other CSS properties that
support numeric values.
javascript
Copy code
$('#myDiv').animate({ left: '+=100px' // Move the element 100px to the right }, 1000);
javascript
Copy code
$('#myDiv').animate({ top: '+=50px', // Move the element 50px down left: '+=100px', //
Move the element 100px to the right opacity: 0.3 // Fade out the element to 30% opacity },
1500); // Duration: 1500ms (1.5 seconds)
8. Stopping Animations
You can stop animations using .stop(). This stops the current animation and optionally prevents the queue from continuing.
javascript
Copy code
$('#myDiv').stop(true, true); // Stops the animation, clears the queue, and jumps to the end
The first true argument stops the current animation immediately.
The second true clears any queued animations.
9. Custom Easing (Using jQuery UI)
If you need additional easing functions, you can include the jQuery UI library, which provides a wider range of easing functions
like easeIn, easeOut, easeInOut, etc.
javascript
Copy code
$('#myDiv').animate({ left: '200px' }, 1000, 'easeOutBounce');
You need to include the jQuery UI script to use these advanced easing functions.