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

WePY cloud development practice in Linux command query applet

Hello everyone, today I will share with you the W...

Three ways to parse QR codes using javascript

Table of contents 1. Use JavaScript to parse the ...

Vue project @change multiple parameters to pass multiple events

First, there is only one change event. changeleve...

Briefly describe the difference between MySQL and Oracle

1. Oracle is a large database while MySQL is a sm...

Web front-end skills summary (personal practical experience)

1. Today, when I was making a page, I encountered ...

Three implementation methods of Mysql copy table and grant analysis

How to quickly copy a table First, create a table...

Summary of methods to include file contents in HTML files

In the forum, netizens often ask, can I read the ...

How to add file prefixes in batches in Linux

You need to add "gt_" in front of the f...

Summary of MySQL time statistics methods

When doing database statistics, you often need to...

CSS and HTML and front-end technology layer diagram

The relationship between Javascript and DOM is ve...

Easyswoole one-click installation script and pagoda installation error

Frequently asked questions When you are new to ea...

Alpine Docker image font problem solving operations

1. Run fonts, open the font folder, and find the ...

RGBA alpha transparency conversion calculation table

Conversion between rgba and filter values ​​under...

MySQL data aggregation and grouping

We often need to summarize data without actually ...

Example explanation of MySQL foreign key constraints

MySQL's foreign key constraint is used to est...