As the background image shifts, it gradually grows in size

I'm attempting to create an interesting visual effect where a background image moves horizontally and loops seamlessly, creating the illusion of an infinite loop of images.

Using only HTML and CSS, I've run into an issue where the background image gradually increases in size over time without any clear reason why. I suspect that JavaScript or jQuery may be needed to solve this problem, but as a novice in these languages, I find myself stuck and unable to make progress. Here's a link to a CodePen showcasing what I've been able to achieve so far:

https://codepen.io/Agnis/pen/aEQjPB

* {
  margin: 0px;
  padding: 0px;
}

body {
  background-color: white;
}

@-webkit-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@-moz-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@-o-keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}

@keyframes backgroundAnimate {
  from {
    left: 0;
    top: 0;
  }
  to {
    left: -10000px;
    top: -2000px;
  }
}
...
<div id="back"></div>
<div id="front"></div>

The goal is to have the background image move smoothly to the left without increasing in size, contrary to its current behavior. Any assistance would be greatly appreciated!

Answer №1

One reason for this is the increase in size. By using the background-position:cover; property, the image will always fill the element, adjusting its size to match the element's dimensions. So, if the element becomes twice as wide, the image will also become twice as wide.

As your

<div id="back"></div>
continues to grow, the background grows along with it.


It is possible to animate a background image. Below is a simple example:

.elem{
    /* properties here */

    transition-property: background-position;
    transition-duration: 1s;

    animation-name: rotate; 
    animation-duration: 2s; 
    animation-iteration-count: infinite;
    animation-timing-function: linear;
}

@keyframes rotate {
    from { background-position: -100%;}
    to {   background-position:    0%;}
}

Answer №2

By adjusting the left and top properties, you are gradually expanding the element and its background.

Instead, try animating the background-position property.

@keyframes bgMove {
  from {
    background-position: left top;
  }
  to {
    background-position:right bottom
  }
}

Also, don't forget to remove any unnecessary prefixes!

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 could be causing the footer to not show up at the bottom of the page?

I've encountered an issue with my code where the footer appears to be stuck at the bottom of the h2_c div instead of the bottom of the .content div where it should be. I have tried using positioning but it hasn't resolved the problem. Thank You. ...

Tips for effectively deleting a flash object from an HTML page

I recently created a website with AJAX (using jquery) for navigation purposes. The pages on the site slide smoothly, and I have been using remove() to get rid of the old page. Everything was working fine until I encountered an issue where the browser cras ...

Is there a way to horizontally align the content in my footer section?

I am currently exploring React Material UI for the first time. The table I am working with can be found here under the "Custom pagination actions" section. Within this section, there is footer content containing buttons for rows per page, next, previous, ...

Locate and select all visible table rows that have the class "x" within a fieldset, then mark them with

Apologies for the lack of a better title. I have successfully implemented the following code snippet: $('.checkall').unbind('click').click(function () { $(this).parents('fieldset:eq(0)').find(':checkbox').attr( ...

The jQuery function, toggle(), allows for easy toggling

One challenging aspect of my website is handling dynamic divs and switching between two different divs within them. I'm struggling to find a way to do this while still maintaining the original value of the div. I've tried writing some code, but ...

Working with Variables in JavaScript and PHP Scripting Languages

I have a PHP script that displays all the folders (categories) on my website: echo "<ul>"; foreach(glob('category/*', GLOB_ONLYDIR) as $dir) { $dir = str_replace('category/', '', $dir); echo '& ...

Click to shift the div downwards

Currently, I have a piece of javascript applied to a div that directs the user to a specific link: <div style="cursor:pointer;" onclick="location.href='http://www.test.com';"> I am wondering if there is a way to add an effect where, upon ...

Unable to fetch information from Grid to a new window with Form in Extjs 4

Having trouble transferring data from a grid to a form in Extjs 4. I'm attempting to pass the field vid to the form for testing purposes, but I'm unable to do so. Despite trying several examples and ideas, I can't seem to make it work. The ...

Using AJAX to set a session variable in PHP

Upon clicking a div, this JavaScript function is executed: $('.test').click(function(e) { e.preventDefault(); $.ajax({ url: 'ajax.php', type: 'POST', data: {"id": "<? ...

Automatically populate input fields with text based on the option chosen from a dropdown menu

Having some issues here... I want to automatically populate the text input boxes in my form when a username is selected from the dropdown. The user data array is retrieved from a MySQL database. Not sure if my JavaScript code is correct. Here's my arr ...

Animated mosaic pattern menu design

Is there a way to achieve this effect with a sketch? Note: I would like to add hover animation to the borders if possible. I attempted to do it using the following code: clip-path: polygon(0 0, 100% 0, 92% 86%, 6% 100%); However, the shapes created by ...

Loop through XML nodes in Node.js

I am faced with a challenge of extracting specific data from a large XML document. The document can be accessed via the following link: https://pastebin.com/mNXWt7dz My goal is to parse the XML structure in order to retrieve values for each client-mac, cl ...

Encountered a NodeJS error while attempting to locate information in a mongo DB: UnhandledPromiseRejectionWarning

In my MEAN stack application, I am working on implementing a login feature that includes social login functionality. When a new user attempts to log in using Facebook, I need to verify if their Facebook account is already registered in my MongoDB database. ...

React 18 doesn't trigger component re-rendering with redux

In my code, I have implemented a custom hook to handle global data fetching based on user authentication. Here is an example of the hook: const userState = useSelector(state => state.user.state) useEffect(() => { if(userState === "authentic ...

Failed XHR: POST request in Cordova encountered an error

I am currently using cordova to develop a mobile application that includes a basic login form. The issue arises when I input the wrong username and password, as I receive a response from the server stating "INVALID INFORMATION" in XML format. However, upon ...

Utilizing Props to Manage State within Child Components

Is it possible to assign the props received from a Parent Component as the state of a Component? export default class SomeComp extends Component { constructor(props) { super(props); this.state = someProps; // <-- I want to set the ...

Obtaining specific data from the forEach iteration, post-click event with JavaScript

Trying to access the value from a Tree-structured Array of Objects stored in Local Storage, Created a function that retrieves values from local storage using forEach and displays them on the screen. Clicking on the Yes/No button triggers the same functio ...

Issue with Bootstrap carousel not centered on the page

Currently working on a carousel using bootstrap. The goal is to display 5 images at a time and have it advance by 1 slide each time. The main issue I'm facing right now is that the elements are not properly positioned within the container, and the tr ...

The submit function for Ajax is not functioning properly

<script> var markerLatitude; var markerLongitude; function initializeMap() { var centerCoordinates = new google.maps.LatLng(51.8979988098144, -2.0838599205017); var mapOptions = { zo ...

Error in Angular: Unexpected '<' token

After setting up my angular and express app using the angular-cli and express command lines, I successfully built the angular app with the ng build command. However, when attempting to serve it with the express server, I encountered the following error in ...