Is there a way to show a loading icon during the image change process without using jQuery?

How can I add a loading icon to an image change event without using jQuery?

I'm new to the coding world and Stack Overflow community, so please bear with me. I've been searching for a solution to my specific situation but haven't been able to find one.

I have code on a Weebly webpage that changes a still image to an animated GIF on mouseover, and back to the still image on mouseout. The image change works properly, but since the GIF is large, it takes a while to load. However, there is no indication that the image is loading when hovered over. I've tried various methods but none have been successful.

I've attempted:

  • Using CSS image animation as a loader (works well, but unsure where to add it)

  • Using a loader as a CSS background image (still image doesn't disappear on mouseover)

  • Adding a div with a loader over the image (mouseover isn't registered through the div)

  • Using buttons to start/stop and animate them during the load

I know preloading the GIF in the background would consume too much data on mobile, and storing images locally isn't feasible due to constant updates. After extensive research and tutorials, I haven't found a non-jQuery solution that fits my needs. I'd prefer not to use jQuery due to difficulty implementing it into Weebly's embedded HTML box. I hope to achieve this with HTML, CSS, and JavaScript if possible.

Below is an example of the code used in the Weebly embedded HTML box:

<img id="pleasework" 
 src="STILL IMAGE URL HERE"  
 class="picswap"
 alt="not working"

 onmouseover="document.getElementById('pleasework')
 .src='GIF IMAGE URL HERE'" 

 onmouseout="document.getElementById('pleasework')
 .src='STILL IMAGE URL HERE'" 
/>

I simply want to insert a loading icon between the still and GIF images. It may be a simple fix, but I need guidance on how to proceed. Any help would be greatly appreciated.

P.S. This is my first post on Stack Overflow, so please let me know if I could improve my question. Thank you for your time.

Answer №1

If you want to avoid using jQuery, one simple approach is to overlay a div containing a loader icon on top of the image you wish to swap. Set the css display attribute of this div to none.

<div>&nbsp;<img src="someloader.gif" id="ourLoaderGif" style="display: none;" /></div>

You can achieve the transition from image to loader back to image using vanilla JS and setTimeout. The nbsp prevents the div from collapsing when the loader is hidden, maintaining a smooth layout transition.

function imageOnMouseEnter() {
  //Time in milliseconds to show the loader (enough time for your image to change)
  var imageLoadTime = 1000;

  //Display the loader
  document.getElementById('ourLoaderGif').style.display = '';

  //Change the original image source
  document.getElementById('pleasework').src = 'IMAGE-URL-HERE.png';

  //Set a timeout to hide the loader
  setTimeout(hideLoader, imageLoadTime);
}

function hideLoader() {
  document.getElementById('ourLoaderGif').style.display = 'none';
}

The downside is that we are currently using a static time interval instead of dynamically adjusting based on the actual image change duration. Nonetheless, this method should guide you in the right direction!

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

Ways to transfer information from the server to the user interface in a WordPress extension

I am currently developing a WordPress plugin and have successfully created a form in the admin panel. Now, I am looking to transfer the data collected from that form to my Frontend JavaScript file. Is there a way to achieve this, and if so, what steps shou ...

Failure to trigger the callback for mongoose.connection.once('open') event

Currently, I am in the process of setting up a custom Node server using Next.js. Although I'm utilizing Next.js this time around, it should not affect the outcome. In my previous applications, I always relied on mongoose.connection.once('open&ap ...

Featuring my Cookie page prominently on my website's homepage

I am facing a simple issue: I have a cookie verification page (cookies_check.php) that I want to include on my home page (accueil.php). So, currently, the only code on accueil.php is: <?php include ('Controleur/cookies_check.php'); ?> He ...

Attempting to send a formik form to a specified action URL

Seeking assistance with a simple fix as I navigate through the learning process. I have created an action called addTenant() that is supposed to receive the state and use it to dispatch a post API call, like so: export const addTenant = (tenant) => (di ...

How to use $$[n] in Spectron/WebdriverIO to target the nth child element instead of the selector

Attempting to utilize Spectron for testing my Electron application has been challenging. According to the documentation, in order to locate the nth child element, you can either use an nth-child selector or retrieve all children that match a selector using ...

The change() function in jQuery appears to have a glitch where it does not properly execute during the initial onclick event, but functions correctly for

Here is the button code snippet: <li class="cnt-li"> <button class="btn-container" title="Add Container" data-toggle="modal" data-target=".add-ctn-modal"> <img src="images/add.png" alt="Add Container"> </button> </li> ...

Hover over the text below to see an image appear in its place

I'm looking to swap out some text with an image when hovering over it, using basic HTML/CSS. NO: show the image below the text NO: display the text on top of the image NO: replace one image with another YES: replace the text with an image ...

Solving the right-to-left orientation issue in the Bootstrap navbar

I have implemented a navbar using the code below, which aligns well from the right side. However, the menu items are sorted in ltr order and I need them to be in rtl order, so that "Dropdown 1" starts from the right side of the page. Currently, it appears ...

In what way does the map assign the new value in this scenario?

I have an array named this.list and the goal is to iterate over its items and assign new values to them: this.list = this.list.map(item => { if (item.id === target.id) { item.dataX = parseFloat(target.getAttribute('data-x')) item.da ...

Creating a route-guard in a Vue.js/Firebase authentication system for secure navigation

I have created a Vue.js/Firebase authentication interface following a tutorial. The app is mostly functioning properly, but I am experiencing issues with the route guard. Upon signing in and redirecting from "http://localhost:8080/home" to "http://localh ...

What steps should be taken to correct the misalignment of the closing x symbol in the alert box?

After making adjustments to the line height, the closing x mark on the right now aligns closer to the bottom of the alert message box. I am currently utilizing Bootstrap version 5.0.1. Is there a way for me to vertically center the x mark? Thank you. CSS: ...

Obtaining a file from Firestore using cloud functions

I have experimented with various approaches, like: const admin = require("firebase-admin"); admin.initializeApp(); const db = admin.firestore(); const docRef = db.collection("users").doc(dynamicDocID).get() const docRef = db.collectio ...

Modify the color of the div element after an ajax function is executed

My original concept involves choosing dates from a calendar, sending those selected dates through ajax, and then displaying only the chosen dates on the calendar as holidays. I aim to highlight these selected dates in a different color by querying the data ...

Using Nextjs Image as the Background for Layout

I have a question regarding implementing a common background image for all pages in my app. Currently, I have the main page.js code that displays an image as the background using Next Image component. However, I would like this background to be present thr ...

Select a random option from the dropdown menu

I have the following script: <script> function $(id) { return document.getElementById(id); } function getImage() { $('loader').src='/misc/images/loading.gif'; var url = "http://mouse ...

What is the process for incorporating Material-UI into a JSFiddle project?

I'm having trouble figuring out how to load and use the Material UI script on platforms like JSFiddle or SO's code editor, even though I can add it with NPM. Check out this JSFiddle example <script src="https://unpkg.com/@material-ui/core/um ...

IE8 is proving to be a major hurdle for the successful operation of AngularJS $http

Currently, I am faced with the challenge of creating an Angular application that needs to be compatible with IE8. However, I'm encountering difficulties in establishing a connection with the server. Surprisingly, whenever I attempt a $http.get, the en ...

Incorporate a gradient into a Vuetify v-card background image

<v-card :img="require('@/core/assets/homeBg.png')" > </v-card> Although the image is currently displaying properly, I am interested in finding a way to add a gradient effect to it. (Specifically, I aim to darken the imag ...

Retain values of JavaScript variables acquired by their IDs even after a page refresh

I have a JavaScript function that retrieves HTML input values and inserts them into a table. However, when I refresh the page, those values are reset to null. How can I prevent this and keep the values visible after a refresh? I believe setting and getting ...

Using PHP, HTML5, and AJAX to extract checkbox data from a contact form

Recently, I set up a PHP/AJAX contact form that successfully sends information like email addresses, names, and message texts to my inbox. However, I encountered an issue trying to include the checkbox values in the data sent to the form handler. Below is ...