Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda