What is the best way to adjust the sprite's position in real-time

Look at this cool sprite I discovered!

.common-spinner.common-spinner-40x55 {
    height: 55px;
    width: 40px;
}

.common-spinner {
    background: url("images/loading-sprite.png") no-repeat scroll 100% 100% transparent;
}

<div class="loading">
<div class="common-spinner common-spinner-40x55" style="background-position: 0px 0px;"></div>
</div>

Has anyone figured out how to create a loading animation using this? I attempted to change the position in a for loop like this:

for(i=0; i<=720;) {
    $('.common-spinner').css('background-position', '-'+i+'px 0px');
    i=i+20;
}

However, there seems to be no animation. Could it be moving too quickly?

Any suggestions on how to make this work?

Best,

I have shared the code on jsfiddle with Erik Hesselink's solution

http://jsfiddle.net/X7tGb/

Answer №1

In order to view the animation, it is necessary to exit the Javascript execution thread. This can be accomplished by implementing timeouts. Here is an example:

function adjustBackgroundPosition (pixels)
{
  return function ()
  {
    $('.common-spinner').css('background-position', '-' + pixels + 'px 0px');
    if (pixels < 720) setTimeout(adjustBackgroundPosition(pixels + 20), 100);
  }
}

setTimeout(adjustBackgroundPosition(0), 100);

Answer №2

http://example.com/jquery-sprite-animation/

If you're interested in JavaScript sprite animation, consider searching on Google as there are numerous resources available. The link above is just one example with a live demo.

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

``There seems to be a problem with the ngb time picker when using the up and

Currently, I am utilizing Bootstrap 4 and NG Bootstrap time picker for a project in Angular 10. Despite correctly adding all the code, I have encountered an issue where the up and down arrows on the time picker are not functioning as expected. Below is a s ...

Effortlessly Transform HTML into Plain Text Using jQuery

If I wanted to create a feature on my website where users can input text into a text area and then convert it to HTML by clicking a button, how could I achieve that? I'm looking for functionality similar to what Wordpress offers, where users can enter ...

Troubles with creating promises in Node.js

Currently, I am facing a challenge in Nodejs where I need to execute asynchronous tasks. Specifically, I need to navigate through all levels of a JSON object sequentially but synchronously. I am experimenting with the following code snippet, which is a si ...

Error encountered: The module '@mui/x-data-grid' does not export 'GridActionsCellItem'

I'm facing an issue while trying to import 'GridActionsCellItem' from '@mui/x-data-grid'. Here's the code: import { GridActionsCellItem } from '@mui/x-data-grid'; An error message pops up indicating: Attempted impor ...

Difficulty encountered with fetching results using jQuery autocomplete with AJAX as the data source

My autocomplete feature is not working properly with my ajax data source. Here is my code: $("#id_q").autocomplete({ source: function (request, response) { $.ajax({ url: "/search/autocomplete/", dataType: "jsonp", ...

Refresh text displayed on a button with the help of setInterval

I need help updating the text on a button with the id fixed-button at regular intervals. Here is the code I am currently using: <script type="text/javascript"> $(function() { $('#fixed-button').setInterval(function() { ...

What is the best way to make translateX transition function in Firefox?

I am encountering an issue with my code working in Chrome but not Firefox. Can anyone offer suggestions on how I can address this problem? The intended functionality is for the door to open when hovered over and for the landscape image to scroll across. Ho ...

Tips for eliminating the entire link from the footer section

Whenever a link is hovered over, a white box displaying the full URL appears at the bottom left of the browser. Is there a way to remove this feature from my website? I appreciate any assistance with this issue. Thank you! ...

Exploring the world of BDD with Node.js and Angular in an actual web

Currently, I am in the midst of developing an application using nodeJs + yo angular-fullstck, starting with BDD and TDD practices. After searching through multiple resources, I have decided to utilize cucumber-js and Jasmin with the karma runner for testi ...

encountering a Zend Framework error

Looking for help with printing data after an inner join of two tables in table format. I encountered the following error in the code below: Parse error: syntax error, unexpected end of file in /home/bina/public_html/studentzend/application/views/scri ...

Creating CSS styles for fluid width divs with equal height and background images

Looking to create a layout with two side-by-side divs of equal width - one containing an image and the other dynamic text that can vary in height from 400px to 550px based on input. The goal is for the image to align flush at the top and bottom with the te ...

When using the table-layout=fixed attribute in IE7, if one column does not have a specified width property, the table's width will

Is there a way to make a table render with minimal width based on its content? I'm facing an issue with IE7 where it insists on expanding the table to 100% width. The code snippet below works fine in Firefox and IE8, but not in IE7: <!DOCTYPE HTM ...

Retrieve a variety of items and pass them to a view using the mssql module in Node

I'm facing an issue while trying to retrieve data from multiple tables and pass them to my view. Below is the snippet of code I've written that's causing the error. router.get('/', function(req, res, next) { sql.connect(config ...

The Node.js application successfully receives an emit event from the browser, however, the browser does not receive any emit

I'm having trouble understanding why the node.js server can connect and receive an emit() from the browser, but when I try to emit() back from node.js it doesn't reach the browser. Am I overlooking something here? The console log displays "Test ...

Preventing redirection post-AJAX call using jQuery

Currently, I am attempting to implement a basic form submission using AJAX to send data to submit.php for database storage. However, upon submission, the page redirects to submit.php instead of staying on the current page. How can I make it submit without ...

Modify the shading of the mesh generated with the face3 method

I have utilized face 3 and three js to generate a mesh, but the expected color is not displaying correctly on the mesh. Below is the code snippet I used: var geometry = new THREE.Geometry(); var f = 0; for (var i = 0; i < data.real.length ...

BeginForm in MVC: When Data Doesn't Get Posted

I'm having an issue with my MVC BeginForm code. I can't seem to access the value of input controls in my controller. Is there something wrong with my setup? using (Ajax.BeginForm("CreateApp", "App", new AjaxOptions { UpdateTargetId = "my-modal-d ...

Having trouble getting $compile to work in an injected cshtml file with Angular

Summary I am currently working on a large application where everything is designed to be very generic for easy expansion. One of the components I am focusing on is the dialog. Before suggesting alternatives like using ngInclude or angular templates, let m ...

Using the previous page button will cause the current page button to have various states

Whenever I use the previous page browser button, the current page's button state does not reset. This causes multiple menu items to appear as if they are in their 'current' state when the previous page is revisited. If I hover over the &apos ...

ISP Blocks Flash Access

My innovative set of flash games has been adopted by numerous schools throughout the UK. However, I recently encountered an issue where one school was unable to load these Flash games... After reaching out to the school's Internet Service Provider (S ...