0

How can I enable/disable an animation from inline CSS

.loading-bar {
  display: inline-block;
  margin-left: 5px;
  width: 4px;
  height: 16px;
  border-radius: 4px;
  /* transform: rotate(0deg) skew(15deg); */
  /* -webkit-transform: rotate(0deg) skew(15deg); */
  /* animation: loading 1s ease-in-out infinite; */
}

Actually, I came up with this function which works, but rely on CSS index that I had to put it 0 for using document.styleSheets[0].rules

function ani(x) {
  var el;
  if (document.styleSheets[0].rules)
    el = document.styleSheets[0].rules[0];
  else if (document.styleSheets[0].cssRules)
    el = document.styleSheets[0].cssRules[0];
  el.style.animation = (x ? 'loading 1s ease-in-out infinite' : '');
}

There is a more elegant way? Thank you.

Ashfaqur_Rahman
  • 140
  • 1
  • 9
yo3hcv
  • 1,531
  • 2
  • 17
  • 27

0 Answers0