Day 104: animation with registered custom properties

All major browsers except Firefox (coming soon!) support the @property at-rule. It enables you to do things you previously couldn’t, like animating custom properties.

Let’s say you have two animations. One fades an element; the other moves it. Based on their motion preference, users see one or the other.

.banner {
    --animation: fade;

    animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  }

  @media(prefers-reduced-motion: no-preference) {
    .banner {
      --animation: move;
    }
  }

  @keyframes move {
    from { translate: 100vw; }
    to   { translate: 0; }
  }

  @keyframes fade {
    from { opacity: 0; }
    to   { opacity: 1; }
  }

That works nice and well, but if you use custom properties in your keyframe animations instead of redeclaring regular properties, it doesn’t work anymore because you’re trying to animate string values.

.banner {
    --animation: fade;

    translate: var(--position) 0;
    opacity: var(--opacity);
    animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  }

  @media(prefers-reduced-motion: no-preference) {
    .banner {
      --animation: move;
    }
  }

  @keyframes move {
    from { --position: 100vw; }
    to   { --position: 0; }
  }

  @keyframes fade {
    from { --opacity: 0; }
    to   { --opacity: 1; }
  }

With the @property at-rule you can turn the two string values into a <length> and a <number>, which makes them animatable.

@property --position {
  syntax: "<length>";
  inherits: false;
  initial-value: 0;
}

@property --opacity {
  syntax: "<number>";
  inherits: false;
  initial-value: 1;
}

@keyframes move {
  from { --position: 100vw; }
  to   { --position: 0; }
}

@keyframes fade {
  from { --opacity: 0; }
  to   { --opacity: 1; }
}

My blog doesn’t support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović

All major browsers except Firefox (coming soon!) support the @property at-rule. It enables you to do things you previously couldn't, like animating custom properties.

Let's say you have two animations. One fades an element; the other moves it. Based on their motion preference, users see one or the other.

.banner {
    --animation: fade;

    animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  }

  @media(prefers-reduced-motion: no-preference) {
    .banner {
      --animation: move;
    }
  }

  @keyframes move {
    from { translate: 100vw; }
    to   { translate: 0; }
  }

  @keyframes fade {
    from { opacity: 0; }
    to   { opacity: 1; }
  }

That works nice and well, but if you use custom properties in your keyframe animations instead of redeclaring regular properties, it doesn't work anymore because you're trying to animate string values.

.banner {
    --animation: fade;

    translate: var(--position) 0;
    opacity: var(--opacity);
    animation: var(--animation) 3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  }

  @media(prefers-reduced-motion: no-preference) {
    .banner {
      --animation: move;
    }
  }

  @keyframes move {
    from { --position: 100vw; }
    to   { --position: 0; }
  }

  @keyframes fade {
    from { --opacity: 0; }
    to   { --opacity: 1; }
  }

With the @property at-rule you can turn the two string values into a <length> and a <number>, which makes them animatable.

@property --position {
  syntax: "<length>";
  inherits: false;
  initial-value: 0;
}

@property --opacity {
  syntax: "<number>";
  inherits: false;
  initial-value: 1;
}

@keyframes move {
  from { --position: 100vw; }
  to   { --position: 0; }
}

@keyframes fade {
  from { --opacity: 0; }
  to   { --opacity: 1; }
}

My blog doesn't support comments yet, but you can reply via blog@matuzo.at.


This content originally appeared on Manuel Matuzović - Blog and was authored by Manuel Matuzović


Print Share Comment Cite Upload Translate Updates
APA

Manuel Matuzović | Sciencx (2023-11-02T00:00:00+00:00) Day 104: animation with registered custom properties. Retrieved from https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/

MLA
" » Day 104: animation with registered custom properties." Manuel Matuzović | Sciencx - Thursday November 2, 2023, https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/
HARVARD
Manuel Matuzović | Sciencx Thursday November 2, 2023 » Day 104: animation with registered custom properties., viewed ,<https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/>
VANCOUVER
Manuel Matuzović | Sciencx - » Day 104: animation with registered custom properties. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/
CHICAGO
" » Day 104: animation with registered custom properties." Manuel Matuzović | Sciencx - Accessed . https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/
IEEE
" » Day 104: animation with registered custom properties." Manuel Matuzović | Sciencx [Online]. Available: https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/. [Accessed: ]
rf:citation
» Day 104: animation with registered custom properties | Manuel Matuzović | Sciencx | https://www.scien.cx/2023/11/02/day-104-animation-with-registered-custom-properties/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.