`There is a delay in rendering the background image on Chrome`

Once I apply a class to my button element using JavaScript, the background image (.gif) that is supposed to display afterwards takes an unusually long time to render.

The button serves as a form submission. Upon being clicked, a class of "clicked" is dynamically added to it through JavaScript. The CSS style dictates that when the button has the class "clicked," it should show a loading animation as its background image. However, this animation often appears delayed (about 0.5-1.0s after clicking), sometimes not even appearing before the page reloads following a submit. Even when pausing the JavaScript execution after adding the class and inspecting the DOM using developer tools, the class is already present but the image fails to display in most cases.

To replicate this issue, here's a fiddle demonstrating the behavior: http://codepen.io/rjk/pen/rGntD

This problem seems to be specific to the Chrome browser, as Firefox shows everything instantly as expected.

If anyone has insight into how to prompt Chrome to immediately reflect the change and eliminate this unpredictability, your input would be greatly appreciated.


A simplified version of the JavaScript code causing this issue is as follows:

function loadingClickHandle(button, style) {
  jQuery(button).addClass(style + "-clicked");
}

loadingClickHandle(this, "button-blue");
$("form:first").submit();

Answer №1

If you're looking to optimize load times by converting images to base64 strings, here's a solution that might help. By encoding the image data directly into the HTML, you can eliminate the need for extra server requests and reduce latency.

It's worth noting, however, that while this method works well for smaller images, it can actually increase the size of the data due to the overhead of base64 encoding.

According to estimates, Base64-encoded binary data is approximately 1.37 times larger than the original data size, plus an additional 814 bytes for headers.

For more information about base64 encoding, check out this link.

The snippet provided below demonstrates how you can apply this technique in your code:

.blue-button {
  background-color: #1b9bdd;
  color: white;
  height: 30px;
  padding-left: 14px;
  padding-right: 14px;
}

.blue-button.clicked {
  background-image: url(data:image/gif;base64,R0lGODlhEAAQAPcX...); /*truncated for brevity*/
  background-repeat: no-repeat;
  background-position: center;
  color: transparent;
}

You can view a live demo of this implementation on CodePen.

To convert your images to base64 strings, consider using a tool like this one.

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 there a way in jqGrid to invoke a function once the subGridRowCollapsed operation finishes?

I'm currently working with jqGrid's subgrids and I need a solution for invoking a specific method after collapsing a subgrid. Currently, my implementation is as follows: subGridRowCollapsed: function (subgrid_id, row_id) { adjust_slider_posi ...

Mastering the Art of Utilizing $(this) in jQuery

$(".item_listing").hover(function(){ $(".overlay_items").display(); },function(){ $(".overlay_items").hide(); } ); This is my jQuery code. I am trying to display the "overlay_items" div when the "item_listing" div is hovered ov ...

When executing multiple promises in parallel, the function Promise.all() does not support the spread() method

I'm attempting to simultaneously run two promises using sequelize, and then display the results in a .ejs template. However, I keep encountering the following error: Promise.all(...).spread is not a function Below is my code snippet: var environme ...

Allow users to interact with table rows by making them clickable and sending a post parameter to a jQuery

After creating a table and populating it with elements using JSTL tags and EL expressions, the next step is to make each row clickable. This can be achieved by implementing the following function: $("tr").click(function() { window.location.href = $(th ...

The occurrence of an unanticipated character '#' was found

I'm currently facing an issue while using webpack to load dependencies. Whenever I execute the npm run dev command, I encounter the following error: Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: ...

The function of the React index key appears to be malfunctioning within the map function

I've been encountering issues despite using the index key and also attempted utilizing a unique id from JSON data, but unfortunately haven't found a solution yet. ERROR Warning: Each child in a list should have a unique "key" prop. const fa ...

2-panel accordion interface

Hello there, I've encountered a problem that seems to be related to CSS. Here is the project I'm currently working on: My main focus is on the News accordion menu. The goal is to display a small image (50x50 with padding) next to a large headlin ...

Update the array with the new data by setting the state

Is it possible to edit and save data in an array while iterating through it using this.state.data.map()? If so, what is the best approach to achieve this? Check out this live example for reference. Below is a sample code snippet: class App extends React ...

Sending a massive video file to a node.js server and saving it in AWS S3 with ReactJS

I am currently in the process of creating an OTT platform, but I have encountered a problem while attempting to upload large files to the server. I initially attempted to store the file in a temporary folder using multer and then utilize aws-sdk s3.upload. ...

Locate a jquery element that is within a parent element containing specific text

This is the format I currently have: <td width="270px"> <img class="bullet" border="0" valign="top" src="gray-bullet.gif"> this is the text </td> Can someone provide me with a query to specifically target the img element with the class ...

Step by step guide to verifying email addresses with Selenium WebDriver

A feature in my EXTJS application includes a page with an Email button. When this button is clicked, it generates a link to the page contents and opens the default email client with this link in the body. Upon inspecting the DOM, I found that the Email bu ...

Updating content on a webpage via AJAX request without altering the original source code

Within the body of our document, referred to as "form.php," we have the following components: At the head section, there is a JavaScript code block: <script> function showUser(str) { if (str == "") { document.getElementById("txtHint").i ...

What is the method for invoking an imported function in a template?

Let's analyze the code snippet below, which utilizes a method and an imported function: <template> <div> {{sayHello()}} {{sayDate()}} {{DateTime.now()}} <div> </template> <script> import {DateTime} from & ...

How can I trigger a row click event while a TextInput is in focus? (react-native)

Whenever I tap on a ListView item, the TouchableOpacity functions properly. However, when a TextInput is in focus, it requires two taps for it to work - the first tap only removes the focus from the TextInput. Is there a way to make it work without havin ...

How can an external style sheet be inserted into an iframe?

My website features an editor on one side and an iframe on the other. Currently, anytime HTML code is entered into the editor and a keyup event occurs, the iframe updates automatically. I am looking to add default styling to the code entered in the editor ...

Get the docx file as a blob

When sending a docx file from the backend using Express, the code looks like this: module.exports = (req, res) => { res.status(200).sendFile(__dirname+"/output.docx") } To download and save the file as a blob in Angular, the following code snippet i ...

Using the <!DOCTYPE html> tag seems to cause issues with the measurements of div elements

Currently in the process of developing a website, I created this div: #container #content .imagewindow { width: 560; height: 380; background-color: #1E1640; } After adding the doctype html tag, the browser appears to be disregarding my styling commands ...

Issue with Material-UI: Unforeseen TypeError arises while toggling between two Drawer elements

Note: I am utilizing material-ui 3.3.0 My goal is to have two <Drawer> components displayed on a page, with one sliding in from the left and the other from the right. In an <AppBar>, there are two <Button> components that toggle the visi ...

Updating the logo image on mobile devices using responsive CSS styling

My goal is to show a different image to users on mobile devices using only CSS, as I am unable to access the HTML code. On mobile, I used display:none for the header-logo-image img { and then added a background-url to the div to successfully display my alt ...

How can I display an ngx spinner after a delay of 1 second?

I am uncertain about the answer I came across on this platform. intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const time = 900; const spinnerLogic = () => { if (this.isRequestServed ...