JQuery fadeIn() function: change the transparency of an element

Table of contents:

JQuery fadeIn() function: change the transparency of an element
JQuery fadeIn() function: change the transparency of an element
Anonim

Animation on modern web pages has long been taken for granted. Dissolving elements, flying messages, sliding image galleries improve the user experience and increase the performance of the site. In the popular jQuery javascript library, fadeIn() is one of the basic animation functions. It controls the transparency of the blocks.

Fade effects

The fadeIn() method ensures that the transparency of the element passed to it changes to 100%. In the jQuery library, it is paired with the fadeOut() function, with which you can reset the transparency and thereby "dissolve" the element.

A similar effect can be achieved using the fadeTo() method, the range of influence of which is even wider. FadeTo() can set any transparency value between 0 and 1.

jQuery fadeIn() function
jQuery fadeIn() function

Method syntax

The jQuery fadeIn() method is called in the context of the element whose transparency needs to be changed. Can take from zero to three parameters:


element.fadeIn(); element.fadeIn(duration); element.fadeIn(duration, callback); element.fadeIn(duration, easing, callback); element.fadeIn(config);

The

duration argument determines the duration of the animation. This can be a number representing the number of milliseconds, or one of the keywords:

  • 'fast' (200ms);
  • 'slow' (600ms);

If

duration is not set, it will default to 400 milliseconds.

The

callback parameter specifies a function that will be called immediately after the animation ends. The callback function does not accept any parameters. The this variable inside it refers to the DOM node being modified.

jQuery fadeout() function
jQuery fadeout() function

The

easing

argument controls the animation's timing function, that is, its speed over time. With it, you can speed up the beginning and slow down the end of the animation, or make it uniform throughout. The value of the argument is a string containing the keyword, the default is the function

'swing'.

In the following example, jQuery fadeIn() will uniformly increase the transparency of an element with class.block for one second, after which it will print a message to the console:


$('.block').fadeIn(1000, linear, function() { console.log('Animation complete'); });

If the listed parameters are not enough, you can pass the

config object to the method, which can contain up to 11 different settings.

Callback functions

The callback parameter passed to the jQuery fadeIn() method is a very useful option, as the property change is done asynchronously without blocking the overall code flow.


const callback=function() { console.log('Animation completed'); }; $('.element').fadeIn(1000); callback();

In this example, the function will run immediately after the animation starts, without waiting for the element to fully appear.

For everything to work as intended, you should use the

callback argument to catch the end of the animation:


const callback=function() { console.log('Animation completed'); }; $('.element').fadeIn(1000, callback);

Now the message will only be printed to the console when the element is fully visible.

Full transparency and element hiding

As you know, the zero value of the

opacity property does not remove the element from the page, but only makes it invisible. This behavior is not good if we want to hide some block.

So jQuery's fade methods fadeIn(), fadeTo() and fadeOut() work with transparency in combination with the

display

property. A completely transparent element is hidden using the rule

display: none, and this rule is canceled before it appears.

Popular topic

Editor's choice

  • The Psijic Order from The Elder Scrolls - who are they?
    The Psijic Order from The Elder Scrolls - who are they?

    The Psijics are the name of a mysterious and ancient Order from the Elder Scrolls game universe. Its members practice magic and are notorious for being at odds with the Thalmor. Even more interesting information about the Psijic Order can be found in today's guide

  • Three easy ways to turn off notifications in Viber
    Three easy ways to turn off notifications in Viber

    "Viber" today is one of the most popular and widespread instant messengers. Sometimes a situation arises that the sound of constantly incoming messages bothers or distracts a person from important matters. How to turn off notifications in Viber? The article presents three of the simplest and easiest ways to do this, and they will help you set the silent mode on both Android and iOS

  • How to advertise on Instagram yourself?
    How to advertise on Instagram yourself?

    One of the most effective methods of website promotion and business development at the moment is advertising on social networks. Social networks, where people actively communicate, are more willing to pay attention to useful posts, join groups, like posts and make reposts, are an almost inexhaustible source of traffic. A properly built advertising campaign will allow you to get new customers, keep the attention of existing ones and again win over those who were dissatisfied with the service

  • Google - what is it?
    Google - what is it?

    The article talks about the multidisciplinary corporation Google. Its services and the company's work as a device manufacturer are considered

  • How to remove a user from important friends on VKontakte: all ways
    How to remove a user from important friends on VKontakte: all ways

    Important friends of "VKontakte" are determined automatically. When friends, family, and a loved one appear first on the list, this is convenient. But relationships can change for various reasons, and the need to constantly see a person’s page may disappear. In this article, we will tell you how to remove from important VKontakte friends those users whom you do not want to see at the top of the list