jQuery Sliding Effects

jQuery slideUp() and slideDown() Methods

The jQuery slideUp() and slideDown() methods are employed to conceal or reveal HTML elements by smoothly decreasing or increasing their height (i.e., sliding them up or down).

<script>
$(document).ready(function(){
// Slide up displayed paragraphs
$(".up-btn").click(function(){
$("p").slideUp();
});

// Slide down hidden paragraphs
$(".down-btn").click(function(){
$("p").slideDown();
});
});
</script>

Similar to other jQuery effects methods, you have the option to specify a duration or speed parameter for the slideUp() and slideDown() methods. This parameter determines the duration of the slide animation. Durations can be defined using predefined strings like 'slow' or 'fast', or in milliseconds, where higher values result in slower animations.

<script>
$(document).ready(function(){
// Sliding up displayed paragraphs with different speeds
$(".up-btn").click(function(){
$("p.normal").slideUp();
$("p.fast").slideUp("fast");
$("p.slow").slideUp("slow");
$("p.very-fast").slideUp(50);
$("p.very-slow").slideUp(2000);
});

// Sliding down hidden paragraphs with different speeds
$(".down-btn").click(function(){
$("p.normal").slideDown();
$("p.fast").slideDown("fast");
$("p.slow").slideDown("slow");
$("p.very-fast").slideDown(50);
$("p.very-slow").slideDown(2000);
});
});
</script>

You can also define a callback function to run after the slideUp() or slideDown() method finishes its animation. We'll cover more about callback functions in future chapters.

<script>
$(document).ready(function(){
// Display alert message after sliding up paragraphs
$(".up-btn").click(function(){
$("p").slideUp("slow", function(){
// Code to be executed
alert("The slide-up effect is completed.");
});
});

// Display alert message after sliding down paragraphs
$(".down-btn").click(function(){
$("p").slideDown("slow", function(){
// Code to be executed
alert("The slide-down effect is completed.");
});
});
});
</script>

jQuery slideToggle() Method

The jQuery slideToggle() method toggles the visibility of selected elements by animating their height. If an element is initially displayed, it will slide up; if hidden, it will slide down, effectively toggling between the slideUp() and slideDown() methods.

<script>
$(document).ready(function(){
// Toggles paragraphs display with sliding
$(".toggle-btn").click(function(){
$("p").slideToggle();
});
});
</script>

Similarly, you can also specify a duration parameter for the slideToggle() method, similar to the slideUp() and slideDown() methods, to adjust the speed of the slide toggle animation.

<script>
$(document).ready(function(){
// Slide Toggles paragraphs with different speeds
$(".toggle-btn").click(function(){
$("p.normal").slideToggle();
$("p.fast").slideToggle("fast");
$("p.slow").slideToggle("slow");
$("p.very-fast").slideToggle(50);
$("p.very-slow").slideToggle(2000);
});
});
</script>

Similarly, you can also define a callback function for the slideToggle() method.

<script>
$(document).ready(function(){
// Display alert message after slide toggling paragraphs
$(".toggle-btn").click(function(){
$("p").slideToggle(1000, function(){
// Code to be executed
alert("The slide-toggle effect is completed.");
});
});
});
</script>