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

Detailed explanation of the difference between run/cmd/entrypoint in docker

In Dockerfile, run, cmd, and entrypoint can all b...

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...

A detailed introduction to wget command in Linux

Table of contents First install wget View Help Ma...

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

Detailed tutorial on installing Python 3.8.1 on Linux

This example takes the installation of Python 3.8...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

How to write the parent and child directories of HTML relative paths

How to indicate the parent directory ../ represent...

JS calculates the probability of winning based on the prize weight

Table of contents 1. Example scenario 1.1. Set th...

How to add color mask to background image in CSS3

Some time ago, during development, I encountered ...

Two ways to introduce svg icons in Vue

How to introduce svg icons in Vue Method 1 of int...

Docker binding fixed IP/cross-host container mutual access operation

Preface Previously, static IPs assigned using pip...

How to deploy FastDFS in Docker

Install fastdfs on Docker Mount directory -v /e/f...