Utilizing jQuery's fadeIn() and fadeOut() functions can lead to a significant decrease

I recently added a loading indicator in the form of an animated .svg with fadeIn and fadeOut effects to display it on my webpage alongside an animated canvas. However, I noticed a significant decrease in the performance of my canvas during these operations, to the point where it caused a specific mobile browser to crash.

To address this issue, I switched to using show() and hide() methods for the element instead, which resolved the performance problem but sacrificed some visual appeal.

I'm curious if anyone else has encountered similar performance issues and if there are alternative ways to smoothly fade my indicator in and out without compromising performance. Any suggestions would be greatly appreciated! Thanks!

Answer №1

JQuery's animation techniques are known for their "pessimistic" approach, as they rely on timers to adjust opacity in the DOM step by step, leading to frequent redraws by the browser. This method was initially used before CSS transitions were widely available, making it the go-to technique for achieving such effects.

However, a more efficient approach now exists by utilizing CSS transitions, enabling the browser to handle most of the work seamlessly. Instead of JQuery directly manipulating opacity, you can simply apply a class and let the browser manage the transition between states.

Many modern browsers optimize opacity and transform transitions using GPU acceleration.

setTimeout(function() {
  $('.fader').addClass('show');
  
  $('.fader').on('click', function() {
    $(this).removeClass('show');
  });
});
.fader {
  width: 200px;
  height: 200px;
  background: cornflowerblue;
  
  opacity: 0;
  transition: opacity 2s linear;
}

.show {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fader">I'll fade in, click me to fade out</div>

In the provided example, setTimeout() is utilized to allow the DOM to render a frame with a <div> containing the fader class and an initial opacity of 0. Subsequently, the show class is applied in the next frame, setting the opacity to 1. With a duration of 2 seconds set for the transition, a gradual fade-in effect is achieved.

An additional feature includes a click event that removes the show class, resulting in a smooth fade-out animation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible for me to fetch the image and asynchronously load it into a div using AJAX

I'm trying to figure out how to load images from the href links into a div using ajax. Can anyone provide some guidance on this? I've been searching and it seems like the load() function may not work for loading images in this way? <ul id ...

The Firefox browser is not rendering the website correctly

After successfully programming a website with the FullPage Slider from this source, everything was working fine except in Firefox. Specifically, one section containing a table with image overlay hover effects from these effects library was not functioning ...

To set up MongoDB on your system, execute the following command: `npm install --

I've encountered a problem while attempting to install MongoDB on my personal computer for a Node project. I used the command line and ran npm install --save mongodb. Even though MongoDB appears in the dependencies section of my package.json file with ...

What is the best way to dynamically pass props to the styles hook in Material UI?

Looking for a way to dynamically add background images to div elements? I'm currently iterating over an array of objects and returning divs, but I'm not sure how to set the background image based on the image URL in each object. Can anyone help m ...

Watch the video and follow along with your mouse using jQuery

Wouldn't it be cool to have a video play and follow your mouse pointer when you hover over the red box? And then once you move away, the video stops and disappears. Check out my progress so far: jsfiddle Here's the jQuery code I've used: $ ...

Tips for ensuring an element stays anchored at the bottom even when the keyboard is displayed

I recently encountered a situation on one of my pages where an element positioned at the bottom using absolute positioning was causing issues when the keyboard was opened, as it would move to the middle of the page unexpectedly. While this may seem like a ...

challenge with altering data in transmission when using the GET method

$('.textedit').editable('/webpage/skeleton/edit_text/text/process', { loadurl: '/webpage/skeleton/edit_text/loadraw', loaddata: {id: $(this).attr('id'), client: $(this).data('client')}, submitda ...

Issues with Angular Component not detecting changes in @Input array

I'm dealing with a challenging setup where: The Parent Service (A) is imported in the Parent Component (B). Then, the Parent Component passes an array of Objects to a Child Component (C), which are referenced from the Parent Service (e.g. <child-c ...

Transferring variables between PHP pages seamlessly without the need for refreshing the page

I have a situation involving two pages. The first page, named mainform.php, contains a form where users input test results that are then stored in a database upon submission. Within mainform.php, there is a Jira issue slider tab where users can report any ...

list of images that adjust to fit various screen sizes

I recently delved into learning responsive web design, but I've encountered a small issue. My navigation menu consists of images with varying widths. Here's how it looks: http://jsfiddle.net/yxLLncpb/ When I use the same dimensions for all image ...

Is it possible for me to customize the default angular filter in order to prioritize displaying strings that begin with the search term?

In my current project, we are dealing with a massive list of strings (17,000+ at times) and have implemented an input box for filtering the list. Initially, I used Angular's default filter, but then a request came in to sort the list so that strings s ...

Version 2 of the Microsoft Logo Animation

I made some changes to my Microsoft logo animation project. You can view the updated version on CodePen. Overall, I'm pretty happy with how it turned out except for one issue. I'm struggling to determine the right timing to end the animation so ...

Trouble displaying images from JSON file in AngularJS using ng-src - need help with image loading

While utilizing AngularJS to extract image data from a JSON file, everything seems to work fine for other types of data except images. Why is the ng-src not retrieving any images? I am attempting to load all the images into the div and set the src, alt, a ...

Tips for organizing your JSON Structure within ReactJs

In the given example, I have a JSON structure with information about different airlines. The Airline Name is dynamic and we need to separate the JSON into an expected array format. const arr = [ { Airline: "Goair", Departure: "01:50" ...

Is there a way to retrieve two separate route details using jQuery simultaneously?

Clicking the checkbox should display the Full Name: input type="text" id="demonum" size="05"> <button type="button" onclick="load_doc()">click</button><br><br> <input type="checkbox" id ="check" > The r ...

Insert text within the switch component using Material-UI

Looking to customize Material UI's Switch component with text inside? Check out the image below for an idea of what I'm going for. https://i.stack.imgur.com/KadRZ.png Take a look at my code snippet: import React from 'react'; import S ...

Setting up a ClientBundle in GWT with multiple CssResources

I am currently in the process of updating some older code to GWT 2 and encountering some strange behavior. I have a custom interface that extends ClientBundle, as recommended by the GWT documentation. Within this bundle, I have defined multiple CssResource ...

Using the className prop in a React Component

Struggling to assign a classname to the material-ui Button component. Here are my failed attempts: Attempt 1: attributes.map((attribute, index) => { const classString = 'classes.button' + index; console.log(classString) return ( &l ...

Show the org.json.JSONArray object in a Dojo Grid

In order to retrieve data from the server, I created a servlet that adds the data to a JSONObject and sends it back to the Dojo request.get() function. While I am able to successfully receive the data, I am unsure of how to properly display the JSON conten ...

What is the technique for wrapping a component with a rectangle box in ReactJs?

Do you like the design in this example image? I attempted to create a similar layout using Material UI Box, but unfortunately, it only displays the text without rendering the box itself. Take a look at the code I used: import * as React from 'react& ...