Implementing a background image using CSS callback instead of utilizing the img element

Working with extremely low-powered devices has led me to consider switching to background-images instead of using img tags for various reasons. However, one major issue I've encountered is the lack of a callback associated with background-image loading or failure, which is crucial for these types of devices.

I've come across suggestions to use an image tag's callback to check and delete the resource, but this method may not be reliable enough for my needs. There is concern that without caching support, multiple network requests could be generated for the same image. Additionally, there is always the risk of successful image loading through the img tag while failing with the background-image property.

Is it possible to attach a separate callback to a div element using addEventListener to monitor its network-based CSS requests? Or if not a callback, is there a way to verify that the background image has indeed loaded?


<style>
.class {
  background-image: url('./static/example.jpg');
}
</style>
<div class="foo"></div>

function onSuccess() { // called if ./static/example.jpg loads successfully
}

function onFailure() { // called if ./static/example.jpg fails to load
}

Answer №1

Is it feasible to use JavaScript to load the image and only apply a class to an element once the image has finished loading?

Here's an example of how it could be done:

var myImage = new Image();
myImage.onload = function() {
    // Add class to element
}
myImage.src = '/images/myImage.jpg';

The concept is for JavaScript to initiate the request for the image, wait for it to be cached by the browser, and then apply the designated class to ensure successful loading.

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

What is the best way to incorporate right side padding into a horizontal scroll?

I'm attempting to include padding on the right side of a horizontal scroll so that the last item in the scroll appears in the center of the screen. Here's what I have tried so far. While the left padding works perfectly, I'm puzzled as to w ...

Inquiring about how to make the pieces move on the checker boards I created

I'm having trouble getting my SVG pieces to move on the checkerboard. Can someone please help me figure out how to make them move, even if it's not a valid move? I just want to see them in motion! The important thing is that the pieces stay withi ...

Drop and Drag File Uploading

I'm currently facing some challenges trying to locate what I need and figuring out how to implement it. At the moment, I have a simple PHP file uploader that works by allowing users to click on a custom upload button, select a file, and using JavaScr ...

Ensuring that the CSS aligns correctly with the HTML structure is essential in order to enable the hover

I am looking to have an image appear when a mouse hovers over ONLY the "name" of the food item, and not the "price" which is part of div.child. <div class="menu-block"> <div class ="menu-titles"><span><p class="title">Brunch< ...

Make the image tag a precise circle shape

Is it possible to create a perfect circle image regardless of its original dimensions, such as not being a perfect square like 100px x 100px? .circle { border-radius: 50%; height: 100px; width: 100px; } Using the above code, if the image size ...

"Bootstrap 4 with the ability to display multiple content sections under a

Currently, I am experimenting with bootstrap tabs and my goal is to have a single tab with multiple content divs. I attempted to achieve this by using two data-target values like data-target=".etab-p1, .etabi-img1". However, the solution only seems to work ...

Can you determine the height of an image if only the width and border width are provided?

Currently delving into the world of HTML and CSS, I've successfully configured the img element's width and border width using a style element. However, one aspect that is still puzzling me is how the height is calculated in this scenario. I&apos ...

What could be causing IE8 to keep text centered even though the text-align property is set to left?

When using Internet Explorer 8's own development tools, the text-align property of the element is displayed as left. However, in IE8 standards mode, it appears centered. In quirks mode or IE7 standards mode, everything aligns correctly to the left. h ...

Troubles arise when hovering over the <strong> tag and the <h4> element

While hovering over my h4 tag in the table, everything functions correctly. However, when I hover over the strong tag inside the h4 element, the strong tag inherits the same hover effect as the h4 tag. The structure of each td element within the table is ...

Is there a way to determine which radio button has been chosen using jQuery?

I'm trying to retrieve the value of the selected radio button using jQuery. Can anyone help with this? Currently, I am able to target all radio buttons like so: $("form :radio") But how can I determine which one is actually selected? ...

issue with conditional statement in Vue.js

In my Vue.js code, I have implemented a v-if statement wrapped around a template tag that is supposed to display a specific message only when the signup type is a referral. Within this template tag, there are nested v-if statements for further conditions. ...

Encountering an unexpected token error while attempting to incorporate JQuery into a React JS project

I am currently diving into the world of React.js and attempting to incorporate a side navigation bar on my homepage. However, I encountered an eslint error when trying to implement the sidebar using jQuery code. Below you will find the code snippet for the ...

jQuery and HTML: Need help enabling an <input> or <select> element?

Currently, I am using Mozilla Firefox 14.0.1 and Google Chrome 20.0.1132.57 (which I believe is the latest version). The code I am working with looks like this: http://jsfiddle.net/SVtDj/ Here is what I am trying to accomplish: Type something into inpu ...

Why is my data not being saved to the database when using eloquent?

I am encountering an issue with my form submission. Although the data is present when using dd() on Requests, the save() function does not work as expected, resulting in the data not being stored in the table. I am attempting to add new Users via a backoff ...

Is it possible to sanitize user input without relying on !important?

Utilizing wp_editor, I have implemented a front-end form on my WordPress website for users to post without accessing the admin section. Although it is functional, there is a concern that users may copy and paste content with inline styles that could disrup ...

Can you tell me why one of these Sass codes is throwing an invalid CSS error while the other is not?

My code: @for $j from 1 to 6 { .text-#($j) { font-size: 15px * $j; } } I'm facing an issue where my code doesn't run as expected. In an attempt to fix this, I modified it to use a 'through' keyword instead of 'to' in the loop ...

Is it possible to access the service and 'self' directly from the HTML template?

When working with Angular 6, one method to access component properties from a service is to pass 'self' to the service directly from the component. An example of this implementation is shown below: myComponent.ts public myButton; constructor(p ...

Troubleshooting a Problem with CSS Classes at a Global Level in

I am working on a global class for an HTML page. <div class="wrapper"> <div class="content"></div> </div> Here is the CSS: .div { width:auto; display:block } .content { width:100px; height:50px; } My issue is with the .con ...

Creating text using the Bootstrap grid system

I am currently using Bootstrap and have set up a grid with a circle on the left side. I am now facing an issue where any text I try to add next to the circle overlaps with it. I need the text to not overlap with the circle and vice versa, while also being ...

Conceal the scroll bar while still allowing for scrolling functionality

In this code snippet, I am trying to maintain the scroll position of two blocks by syncing them together. Specifically, I want to hide the scrollbar of the left block while scrolling the right one. If anyone has any suggestions or solutions for achieving ...