Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
dynamically apply css/ sass keyframe animation

I have a compass, and this animation makes the compass needle rotate back and forth a little bit like a real one. For example if it was pointing at 30 degrees, I would apply this. +10 and -10.

@keyframes spin {
    0%   { transform: rotate( 40deg ); }
    50%  { transform: rotate( 20deg ); }
    100% { transform: rotate( 40deg ); }
  }

With these properties

animation-iteration-count: infinite;
animation-duration: 1s;
animation-timing-function: ease-in-out;

The problem is, I only know the degree after its calculated in javascript, so a style or class will need to be applied with js.

My first idea is rounding the degree to the nearest 10 so i can apply one of these animations to it. worst case scenario it will be 5 degrees out. I could do it with 1 degree of accuracy if its not that much stress on the client

$divisions: 36;
@for $i from 0 through $divisions {
  $value: ( 360 / $divisions ) * $i;

  @keyframes spin#{ $value } {
    0%   { transform: rotate( #{ $value + 10 }deg ); }
    50%  { transform: rotate( #{ $value - 10 }deg ); }
    100% { transform: rotate( #{ $value + 10 }deg ); }
  }
}

It declares 36 different keyframe animations. I try to apply an inline style of animation-name: spin40; ( Rounded to the nearest 10 ), but that isn't working. I assume animation name doesnt work as an inline style.

I could declare a class in the same loop and apply that class inline but im also reading this is a really bad idea. And its probably bad practise to declare 36 animations.

I thought I could maybe do it in a mixin, but I cant really, because it would still compile to 36 different animations.

What is the best way I can do this please, many thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!