我正在判断我发现的一个动画的css代码:The code

动画采用背景的第一列,并通过从上到下(背景的左侧)循环通过背景来制作动画.

然而,问题是,我不能为其他三种类型的移动(其他三列)复制这个过程.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .tuna {
      animation: walk-cycle 1s steps(12) infinite;
      background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-90/tuna_sprite.png) 0 0 no-repeat;
      height: 200px;
      width: 400px;
      margin: 100px auto 0;
    }
    
    @keyframes walk-cycle {
      0% {
        background-position: 0 0;
      }
      100% {
        background-position: 0 -2391px;
      }
    }
    
    html {
      text-align: center;
    }
  </style>
</head>

<body>

  <div class="tuna"></div>
</body>

</html>

我划分了图像的高度和宽度,并从第二列移动了动画的开始,但无论我在动画中添加多高或多宽,它都不起作用.

@keyframes walk-cycle {  
  0% {background-position: 400 0; } 
  100% {background-position: 400 -2391px; } 
}

我认为问题可能出在Backback属性中的"0 0"值,因为它们代表默认的背景位置(左上角),但无论我多少次更改它的值,它都不起作用,它只给我一个空白页面.

background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-90/tuna_sprite.png) 0 0 no-repeat;
            

我试图搜索"0 0"值,但无法找到它们代表的值.

推荐答案

以下是基于您的PNG的通用代码.您所需要做的就是更新变量

.tuna {
  --s: 100px; /* height of the image */
  --n: 12; /* number of frames */
  --i: 0; /* the movement (0,1,2,3) */

  height: var(--s); /* define only the height */
  aspect-ratio: 2; /* you need a width = 2*height */
  background: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-90/tuna_sprite.png);
  background-size: 400% auto; /* we have 4 columns so width = 4*100% and we keep the height auto */
  background-position: calc(var(--i)*100%/3) 0;
  animation: walk-cycle 1s steps(var(--n)) infinite;
}

@keyframes walk-cycle {
  100% {
    background-position: calc(var(--i)*100%/3) calc(-1*var(--n)*var(--s));
  }
}
<div class="tuna"></div>
<div class="tuna" style="--i:1;--n:6"></div>
<div class="tuna" style="--i:2;--n:12"></div>
<div class="tuna" style="--i:3;--n:13"></div>

Css相关问答推荐

基于子元素的子元素数量的样式元素

自定义kendo色彩 Select 器中的清晰按钮

如何提高数学输出的质量?

如何在css中从圆中切出1/n?

使用 Flex 容器,如何在右对齐所有 Flex 子项的同时赋予子图像完整宽度?

为什么删除 AnimatePresence framer.div 时会出现轻微卡顿?

动态覆盖 Vuetify 类

TypeScript 中有效的 CSS 属性名称数组,但带有连字符

根据选项 colored颜色 更改 Select 文本 colored颜色

Tailwind CSS 隐藏和可见

整个列的 CSS Grid Margin Top

从 Angular SCSS 生成 CSS?

flex-start 和基线有什么区别?

在CSS中将文本和 Select 框对齐到相同的宽度?

水平列表项

如何在 javascript 中获取 HTML 元素的样式值?

thead 和 tbody 之间的间距

了解 Bootstrap 3 中的网格类( col-sm-# 和 col-lg-# )

通过 Bootstrap4 对列进行排序

为什么 height:0 不隐藏我的填充