Over a million developers have joined DZone.

How to apply HTML User Interface Effects using jQuery

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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.

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:

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:

speed can be “slow”, “normal” and “fast”. You can also provide number as an argument which represents milliseconds.

Also you can specify a handler function that gets called once the animation is completed.

Also, you may want to implement a toggle functionality where a button click event can trigger toggle effect on some div or table.


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.

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.

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.


Animate HTML component using jQuery

jQuery provides an animate() function that can be used create your own custom animations.

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.

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

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.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Viral Patel. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}