Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 485
Luckyfella Avatar asked Apr 05 '18 09:04

Luckyfella


People also ask

What is the difference between keyframes and Webkit keyframes?

You first describe the animation effect using the @-webkit-keyframes rule. A @-webkit-keyframes block contains rule sets called keyframes. A keyframe defines the style that will be applied for that moment within the animation.

Which CSS property is used to control the steps in CSS animation sequence?

CSS animation-timing-function Property.

How does keyframe animation work CSS?

Each keyframe describes how the animated element should render at a given time during the animation sequence. Since the timing of the animation is defined in the CSS style that configures the animation, keyframes use a <percentage> to indicate the time during the animation sequence at which they take place.


1 Answers

I used to use something like SCSS to generate automatically created names for my keyframes. 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.

like image 147
somethinghere Avatar answered Sep 28 '22 05:09

somethinghere