The fade effect is described as the gradual increase and decrease in the opacity of the selected portion. In other words, the fade effect is termed as the increase and decrease in opacity with respect to time. When this effect is applied with a gradual decrease in the opacity it is known as the fade-out effect. This is the effect that is used to fade out the selected part on page with a selected interval of time. Fade-out and Fade-in effect is wonderful to use in web projects as it emphasizes the styling of the web page.
The fadeOut effect is used to change the level of opacity for the selected elements from visible to hidden. By using this method, the faded element will not occupy any space. We are using the setInterval method and clearInterval method in this logic.
Syntax of in-built functions to be used in codes:
- setInterval(): It takes two parameter as:
setInterval(function_reference, time interval)
- clearInterval(): It takes single parameter as:
clearInterval(parameterof time to stop calling a function)
Approach 1: On page loading, we are calling a function called fadeout(), in which we are using setInterval() method which takes two parameter: one is function reference(which is hide in this case) and other is timer(in number).
In hide function, we are taking property opacity in variable of name opacity and reducing it by 0.1 every time the fadeout function is called.
- In above code, the portion in which fade-out effect is to be done is selected by
id(body in this case).
- window.onload=fadeout, is used to call fadeout() function automatically when the page goes on loading.
- In fadeout() function, one more function of name setInterval() is also calling, which calls hide() function on every interval of 200 milliseconds till clearInterval() is called, clearInterval() will be called when opacity of selected portion becomes zero.
- In hide function, simple logic is written for reducing opacity by 0.1 every time the fadeout() function is called on selected part.In this, getPropertyValue(“opacity”), is used to select the property opacity.
- These works continues till opacity becomes 0.
Note: Opacity lies between 0-1, in this case initial opacity value is taken 1.
Approach-2: In this approach, complete logic is written inside a variable which is done with the help of setInterval() method, here complete function is to be written in place of function reference. This approach is comparatively easy.
Note: Opacity lies between 0-1, in this case, initial opacity value is set to be 1.
- In above code, the portion in which fade out effect is to be done is selected by
id(body in this case).
- window.onload=fadeout, is used to call fadeout() function automatically when page goes on loading.
- In fadeout() function, we are defining our logic in intervalId variable which calls setInterval() method, here instead of giving function reference, whole function is being defined.
- In defined function, every time we are comparing style.opacity with 0, and if it is greater than 0, an operation is to be performed to decrease the opacity by 0.1 value and when this value becomes 0, the clear function will be called automatically.
- This works continues till opacity becomes 0 and clear function is called.
We have taken 0.1 and 200 milliseconds as numerical values, any values are acceptable.
- jQuery | Effect fadeOut() Method
- How to fadeOut and remove a div using jQuery ?
- ReactJS Pure Components
- Deesign a close button using Pure CSS
- CSS | Shadow Effect
- CSS Stencil Effect
- jQuery UI | effect() Method
- Parallax scrolling effect using CSS.
- How to Create a GrayScale Effect using CSS ?
- How to use Explode effect in jQuery ?
- Flipping Window Effect
- How to Create Paradoxical Effect using CSS ?
- How to disable a CSS :hover effect?
- How to create a sliding background effect using CSS ?
- How To Create Carved Text Effect using CSS?
- How to Create Embossed Text Effect using CSS ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.