简体   繁体   English

我们可以将 CSS 关键帧动画限制在一个范围内吗

[英]Can we restrict CSS keyframe animations to a scope

I wonder if it's possible to restrict keyframe animations to a scope based on classnames.我想知道是否可以将关键帧动画限制在基于类名的范围内。 The benefit would be to be able to use the same animation-name multiple times without getting issues.好处是能够多次使用相同的动画名称而不会出现问题。 I couldn't find any infos about that..我找不到任何有关的信息..

In case it's not possible: are there any best practices to handle naming conflicts?如果不可能:是否有处理命名冲突的最佳实践?

I used to use something like SCSS to generate automatically created names for my keyframes.我曾经使用 SCSS 之类的东西为我的关键帧自动生成名称。 They might not be as descriptive, but they ensure uniqueness.它们可能没有描述性,但它们确保了唯一性。 Something like:类似的东西:

$animation-id-count: 0 !global;

@function animation-id {

  $animation-id-count: $animation-id-count + 1;
  @return animation-id-#{$animation-id-count};

}

After this, just use the function in your code like this:在此之后,只需在您的代码中使用该函数,如下所示:

.class {

  $id: animation-id();

  @keyframes #{$id}{
    ...keyframes
  }

  animation: $id 1s infinite;

}

That way if you insert it anywhere else in your SCSS or move it, it will still match the right animation, and it stops namespaces from overlapping in any way.这样,如果您将它插入到 SCSS 中的任何其他位置或移动它,它仍然会匹配正确的动画,并且它会阻止命名空间以任何方式重叠。

Here is a JSS approach (you will need object-hash for that).这是一种 JSS 方法(为此您需要对象哈希)。

The following example shows how to define different animations with respective unique ID based on transform: scale(n) .以下示例展示了如何基于transform: scale(n)定义具有各自唯一 ID 的不同动画。 For that purpose, define a function which returns the keyframes and its ID.为此,定义一个返回关键帧及其 ID 的函数。 The keyframes ID is a custom string suffixed with a hash of the function options (eg the scale factor).关键帧 ID 是一个自定义字符串,后缀为函数选项的散列(例如比例因子)。

(Be careful of CSS custom identifier, eg do not include a . in your ID. See MDN: < custom-ident > .) (注意CSS定制标识,例如不包括.您的帐号见。 MDN:<定制的ident> )。

import hash from "object-hash";

const keyFramesScale = (options = {}) => {

    let { transforms, id, scale } = options;
    transforms = transforms || "";
    scale = scale || 1.25;

    const keyFramesId = `scale${id ? "-" + id : ""}-${hash(options).substring(0, 6)}`;
    const keyFrames = {
        [`@keyframes ${keyFramesId}`]: {
            "100%": {
                transform: `scale(${scale}) ${transforms}`,
            },
            "0%": {
                transform: `scale(1) ${transforms}`,
            }
        }
    };

    return [keyFramesId, keyFrames];
};

How to use it:如何使用它:

const [scaleUpId, keyFramesScaleUp] = keyFramesScale({ scale: 1.25, transforms: "rotate(-30deg)", id: "up" });
const [scaleDownId, keyFramesScaleDown] = keyFramesScale({ scale: 0.75, transforms: "rotate(-30deg)", id: "down" });

// scaleUpId = "scale-up-c61254"
// scaleDownId = "scale-down-6194d5"
// ...

<tag style={{
    ...keyFramesScaleUp,
    ...keyFramesScaleDown,
    ...(!hasTouchScreen && isActive && !isClicked && {
        animation: `${scaleUpId} 0.5s infinite alternate linear`,
        "&:hover": {
            animation: "none",
        },
    }),
    ...(isClicked && {
        animation: `${scaleDownId} .25s 1 linear`,
    }),
}} />

Of course, you can write a more generic function that hashes the whole key frames and assign it an ID based on that.当然,您可以编写一个更通用的函数来对整个关键帧进行散列并根据它为其分配一个 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM