Example of implementing a seamless infinite loop of background using CSS animation

Example of implementing a seamless infinite loop of background using CSS animation

1. Demand

A picture moves from left to right in an infinite loop

2. Code

Since it is applied on mobile devices, rem units are used, and the following problems also occur here.

HTML

<div class="dog"></div>

CSS

.dog {
    width: 5.4rem; \\Image width height: 3.04rem; \\Image height background-image: url(head.jpg);
    background-size: 5.4rem 3.04rem; \\Image width and height background-position: -5.4rem 0;
    animation: run 2s linear infinite;
}

@keyframes run {
    from {background-position: -5.4rem 0;}
    to {background-position: 0 0;}
}

3. Problem

There is no problem on the PC side, but on the mobile side (perhaps due to recalculation of the font size?) you will find that the movement speed is inconsistent with the settings, and the image cannot be seamless.

4. Reasons

Unknown, no relevant information found. It seems that the problem is caused by dynamic calculation of font-size.

5. Solution

After testing, it was found that it is normal to add animation to the image after the file is loaded. So modify the code:

JS

$(document).ready(function(){
  remReSize();
  setTimeout(function() {
    $('.dog').css('animation', 'run 2s linear infinite');
  }, 0);
});

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Possible reasons why the input type="reset" tag in HTML is invalid (does not work).

>>:  Use of Vue3 pages, menus, and routes

Recommend

Implementation of dynamic rem for mobile layout

Dynamic rem 1. First, let’s introduce the current...

How to migrate local mysql to server database

We can use the scp command of Linux (scp cannot b...

HTML+CSS+JS realizes the scrolling gradient effect of the navigation bar

Table of contents First look at the effect: accom...

In-depth explanation of Session and Cookie in Tomcat

Preface HTTP is a stateless communication protoco...

Detailed explanation of MySQL basic operations (Part 2)

Preface This article contains 1. Several major co...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

A super detailed Vue-Router step-by-step tutorial

Table of contents 1. router-view 2. router-link 3...

Example of how to create and run multiple MySQL containers in Docker

1. Use the mysql/mysql-server:latest image to qui...

Detailed tutorial on installing nacos in docker and configuring the database

Environment Preparation Docker environment MySQL ...

Solution to nacos not being able to connect to mysql

reason The mysql version that nacos's pom dep...

How to upload the jar package to nexus via the web page

When using Maven to manage projects, how to uploa...

Linux service monitoring and operation and maintenance

Table of contents 1. Install the psutil package S...

WHMCS V7.4.2 Graphical Installation Tutorial

1. Introduction WHMCS provides an all-in-one solu...

Modify the style of HTML body in JS

Table of contents 1. Original Definition 2. JS op...