0% found this document useful (0 votes)
10 views6 pages

Effects and animation

jquery notes

Uploaded by

gauharjahansd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Effects and animation

jquery notes

Uploaded by

gauharjahansd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

In jQuery, effects and animations are a powerful set of features

that allow you to create dynamic visual interactions by


manipulating the visibility, position, size, opacity, and other
CSS properties of DOM elements. jQuery provides a set of built-in
methods to create smooth transitions and animations without
needing to write complex JavaScript code.
Here’s an overview of the effects and animations you can achieve with jQuery:

1. Basic Visual Effects

These effects are used to modify the appearance or visibility of an


element in various ways. They’re typically simple and can be
applied to all types of elements.

1.1 .show() and .hide()

These methods are used to control the visibility of an element.

 .show(): Makes an element visible.


 .hide(): Hides an element.
javascript
Copy code
// Hide an element $('#myDiv').hide(); // Show an element $('#myDiv').show();

1.2 .toggle()

The .toggle() method alternates between showing and hiding an element.

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()

Fades an element in (increases opacity from 0 to 1).

javascript
Copy code
$('#myDiv').fadeIn(1000); // Fades in over 1 second (1000 ms)

2.2 .fadeOut()

Fades an element out (decreases opacity from 1 to 0).

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()

The .fadeToggle() method alternates between fading in and fading out.

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()

Slides up an element, collapsing it vertically.


javascript
Copy code
$('#myDiv').slideUp(1000); // Slide up over 1 second

3.2 .slideDown()

Slides down an element, expanding it vertically.

javascript
Copy code
$('#myDiv').slideDown(1000); // Slide down over 1 second

3.3 .slideToggle()

Toggles between sliding up and sliding down.

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.

4.1 .animate() Method

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)

4.2 Animating with Easing Functions

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

4.3 Chaining Animations


You can chain multiple animations to run one after the other.

javascript
Copy code
$('#myDiv') .animate({ left: '100px' }, 500) .animate({ top: '100px' }, 500) .fadeOut(500); //
Perform multiple animations in sequence

5. Queueing and Delaying Animations


You can control the order of animations using queues and delays.

5.1 .queue() Method

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);

5.2 .delay() Method

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.

7.1 Move an element

javascript
Copy code
$('#myDiv').animate({ left: '+=100px' // Move the element 100px to the right }, 1000);

7.2 Animate multiple properties

You can animate multiple properties at once:

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.

You might also like