Animate the background image with a float effect using jQuery's timeout feature to create an up

I'm having an issue with a set of elements that have background images applied to them. Specifically, I want the planets in the images (all except the first one) to move slightly up and then back down to create a floating effect.

In my jQuery code, I've created a setInterval function to run a loop every 2 seconds. Inside this loop, I attempted to change the background position of the elements. When I directly set the background position using .css(), it blinks the planets as expected within the 2-second interval. However, when I tried to use the animate() method like so:

$('.header').animate({backgroundPosition: '(0px -400px)'}, 1000 );// additional space here

No actual movement occurred on the page. My goal is to achieve a hover-like effect where the planets move up and then down repeatedly. Here's the relevant jQuery code snippet:

// Moving planets up and down
$(document).ready(function(){
  var i = 0;
  setInterval(function(){

    //$('.header').css('background-position','top 5px center, -155px 200px, left 1255px top 250px, right -15px top 350px');
    
    $('.header').animate({backgroundPosition: '(0px -400px)'}, 1000 );

    i++;    
  },2000);   
});

Answer №1

#stars {
  height: 80vh;
  
  background-image:
    url(//placehold.it/50x50/f00),
    url(//placehold.it/50x50/0f0),
    url(//placehold.it/50x50/00f);
    
  background-position:
    50% 10%,
    30% 20%,
    70% 30%;
    
  background-repeat: no-repeat;
  background-size:
    2%,
    3%,
    4%;
    
  animation: stars 1s 0.5s ease-in-out infinite;
}

@keyframes stars {
  to {
    background-position:
      40% 15%,
      25% 35%,
      60% 40%;
  }
}
<div id="stars">


</div>

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 prevents certain scenarios from being encapsulated within a try/catch block?

Just attempted to handle ENOENT by using a naive approach like this: try { res.sendFile(path); } catch (e) { if (e.code === 'ENOENT') { res.send('placeholder'); } else { throw e; } } Unfortunately, this method is ineffectiv ...

Automatically refresh online user list upon new user login

<?php <div class="someclass"> <ul class="someclass"> foreach ($this->showonline as $key => $friend) { <li> $friend['avatar']; $friend['name']; </l ...

What sets React$Element apart from ReactElement?

Attempting to implement flow with ReactJS and needing to specify types for prop children. The article on Flow + React does not provide any information. Despite encountering examples using ReactElement (e.g), I received an error message from flow stating i ...

Unable to retrieve selected value with AJAX

My select box is set up with some values, and I'm using AJAX to send data to PHP and display that data inside a <div>. However, my code doesn't seem to be working properly. Interestingly, when I used a button to get the value from the sele ...

"Selecting an element through Drag/Drop will result in the element being

INQUIRY I've encountered an issue with a draggable element ($("#draggable")) that interacts with a checkbox ($('#checkable')). When the $('#draggable') is dragged onto a box ($('#droppable')), it checks $(&apos ...

Cease the video playback in the modal when the modal is closed

I've attempted several solutions found on stack overflow, but none of them seem to be effective. When I replace the video tag with an iframe, some solutions work, but the video autoplays again, causing issues with the solution. <div class="ico ...

What is the best way to access the values associated with the keys in an array of objects?

I have an array of objects that has the following structure: import icon1 from '!!raw-loader!../../../icon1.svg'; import icon2 from '!!raw-loader!../../../icon2.svg'; import icon3 from '!!raw-loader!../../../icon3.svg'; import ...

Launching a new popup window using jqModal

I need help with a jqModal popup window scenario. I have one main popup window that I want to close after clicking OK, and then open another popup window. How can I achieve this? The issue I am facing is that the click event is not triggering as expected. ...

An unspecified number of Ajax requests being made within a loop

Here is the dilemma I'm facing, despite trying different recommendations from StackOverflow: Situation: I am utilizing the Gitlab API to display all the "issues" in the bug tracker of a specific project on the system. Challenges: Everything wor ...

Modifying Element Values with JavaScript in Selenium using C#

As a newcomer to automation, I am facing a challenge with automating a web page that has a text field. I initially attempted using driver.FindElement(By.XPath("Xpath of elemnt").SendKeys("Value"); but unfortunately, this method did not work. I then resor ...

Guide on how to use a JavaScript AJAX call to download a text file in Java

I am looking to implement a way to download a text file (e.g. 'something.txt') using an AJAX call rather than just an anchor tag in HTML. Here is the HTML code: <body> <a href="#" id="exportViewRule">Export</a> </body&g ...

What is the best way to adjust the fontSize of text within a table using Material UI?

In my Material UI table, I am attempting to adjust the fontSize of the contents. However, when I try to modify it using the style={{}} component, the changes are not being applied. <Grid><Typography variant="body1">Fiscal Year </Typography&g ...

Is it possible to reverse engineer a UI by starting from its current state and debugging backwards?

When it comes to web development, users often provide screenshots of their "invalid state". Using React, I had a thought about debugging in a different way: starting from the current state: A user sends us a screenshot of an invalid UI state. We use dev ...

Select items displayed next to each other with varying number of columns in HTML

Users can select a medicine for each day of their plan, with the option to choose up to 7 days. I want to dynamically show a corresponding number of Select dropdowns based on the user's selection. body { width: 500px; margin: 0 auto; ...

Is there a way to retrieve the data selected in my modal window when a button is clicked, depending on the v-for value?

As someone who is new to Vue and utilizing Bootstrap modals to showcase product information, I have grid containers that contain a product image, description, and two buttons. One of the buttons, labeled More details >>, is intended to trigger a moda ...

Tips for resolving the error message "An issue occurred with the skill's response" within the Alexa Developer Console

Looking to develop a basic Alexa app where users can interact with voice commands, but encountering an issue with the response in the Test tab of Alexa Developer Console. The error message states "There was a problem with the requested skill's respons ...

Bug in floating elements within a header causing issues with IE6 and IE7

We are currently facing an issue where an anchor tag is floating right inside a header. While it displays correctly on IE8 and Firefox, we are encountering problems with it popping outside the header box. Does anyone have any suggestions on how to prevent ...

The charts created using chartjs-vue display no data

After following some examples to create a test chart, I am facing an issue where the chart renders blank with dummy data. https://i.sstatic.net/RD77S.png My initial suspicion is that maybe the options are not being passed for the lines, but it seems like ...

Calculate the number of items per month by utilizing JSON and the jQuery `each()` function paired with an IF statement

Looking to neatly display sales data per item and month from a JSON object in HTML? You're in the right place! The JSON object contains information on items with their corresponding sales listed by month: var my_object= [{"item_id":"11","month":"Feb ...

Exploring different ways to customize the grid style in Angular 6

Here is the style I am working with: .main-panel{ height: 100%; display: grid; grid-template-rows: 4rem auto; grid-template-columns: 14rem auto; grid-template-areas: 'header header''sidebar body'; } I want to know how to cha ...