How to apply HTML User Interface Effects using jQuery.

jQuery is a small and wonderful JavaScript library that I use extensively in my work. I thought to write a tutorial about achieving UI Effects through jQuery. In this post, I explained how to do fade in/fade out effect using jQuery. This time let us see all the tricks that we can do to manipulate HTML using jQuery.

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.
<input type="text" name="username" id="user"/> <input type="button" onClick="$('#user').hide()" value="Hide Textbox"/>
Code language: HTML, XML (xml)
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:
$("#someid").show();
Code language: JavaScript (javascript)
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) E.g.: $("input").hide("slow"); $("p").show("normal");
Code language: JavaScript (javascript)
speed can be “slow”, “normal” and “fast”. You can also provide number as an argument which represents milliseconds.
$(“.someclass”).hide(2000);
Code language: JavaScript (javascript)
Also you can specify a handler function that gets called once the animation is completed.
$("#id").show("slow", function() { alert("done"); });
Code language: JavaScript (javascript)
Also, you may want to implement a toggle functionality where a button click event can trigger toggle effect on some div or table.
<table id="tableId"> <tr><td> This is demo </td></tr> </table> <input type="button" value="Toggle Table" onClick="$("#tableId").toggle();" />
Code language: HTML, XML (xml)

Slide Up/Slide Down using jQuery

This is a simple effect that slides down or up a container. Just you need to call slideUp() or slideDown() functions.
$("#divId").slideUp(); $("table").slideDown("slow"); $("div").slideUp(1000);
Code language: JavaScript (javascript)
Similar to hide()/show() slideDown()/slideUp() can also be called with arguments to adjust the speed of sliding and optionally a call back function can be called.
$("span").slideDown("slow", function() { alert("Slide Done"); });
Code language: JavaScript (javascript)
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.
$("div#specialDiv").slideToggle("fast");
Code language: JavaScript (javascript)

Animate html component using jQuery

jQuery provides an animate() function that can be used create your own custom animations.
animate(params, speed, easing, callback);
Code language: JavaScript (javascript)
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.
$("p").animate({ height: 'toggle', opacity: 'toggle' }, "slow"); $("p").animate({ left: 50, opacity: 'show' }, 500);
Code language: JavaScript (javascript)
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).
$("p").animate({ opacity: 'show' }, "slow", "easein");
Code language: JavaScript (javascript)
Thus you can leverage jQuery to create a good UI experience on your webpage. Also let me know if you using your own custom built plugins to achieve more UI effects.

View Comments

  • How to use style tag inside the struts tag... We are implementing the UI-slider control using struts... Here we have to hide the list box and show the slider..

    Months From Today
    <strong>Length of Term</strong>

    <!--

    style="position:absolute;left:-999em;top:-999em;">
    24
    36
    48
    60
    72
    84

    -->

  • Hi Vidhya,
    If you want to apply style attribute in any of the struts tags, you shall use styleClass and apply CSS styles using that. You may not directly apply style attribute.
    <html:checkbox styleClass="customClass" />

  • Your site and you will reach the big successes, thank, it was interesting.
    I am from Dominican and also am speaking English, please tell me right I wrote the following sentence: "Apr white papers, case studies, business articles, and blog posts relating to turbotax case."

    With love :-(, Katharine.

  • This is exactly what I need. However, I've been trying to make the information slide up and not down which seems to be the default. I see that you said it is possible but I don't know exactly how to accomplish this. My attempt to alter the code generated unwanted results, interesting but unwanted.

  • I love jQuery, it's a nice summary above, but tbh you could have added some demos like in the jQuery manual or on w3schools.

  • Hi Viral,

    I have use '' then to give an error like 'JavaScript runtime error: The value of the property '$' is null or undefined, not a Function object'

    please give me suggestion to how to resolve it..

    Thnaking You

Share
Published by
Viral Patel
Tags: animate fade effects fadein fadeout JavaScript javascript framework JQuery jQuery Effects show hide effect

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago