Animation using CSS class

Asked by At

I have an HTML component which looks like this. enter image description here

When I add a conditional class to this component two "div"s at the bottom should change its position to the right side of "Primart EMF" text, which looks like this.

enter image description here

I am able to achieve it using display property by changing it from block to inline-block. But this transition is not smooth. I want to add some animation to it, but transition doesn't work on display property.

Can someone please guide me on how to achieve this?

HTML

 <ul class="condition-list edit--mode--off"> <!-- edit--mode--off is a dynamic class-->
  <li class="condition-element condition-element-selected">
    <div class="condition-element-type">Primary EMF</div>
    <div class="condition-element-names">
      <div class="condition-element-name condition-growth">Innovation</div> 
      <div class="condition-element-name condition-achievement">Mastery</div>
    </div>
  </li>
 </ul>

CSS

    .condition-element-type {
        display: inline-block;
        max-width: 71px;
        margin: 2px 10px 0 0;
    }

    .condition-element-names {
        display: inline-block;
    }

    .condition-element-name {
        display: inline-block;
        margin-left: 10px;
        border-radius: 8px;
        color: $color-white;
        font-size: 11px;
        margin: 0 8px 8px 0;
        height: calc(1em + 5px);
        padding: 4px 8px 2px 8px;
        margin: -3px 8px 8px 0;
    }

    .edit--mode--off .condition-element-names {
        display: block !important;
        margin-top: 5px;
    }

1 Answers

2
cypark On Best Solutions

Not sure if this is doable as I see only a part of your code, but it is usually more convenient to use transform and transition for smooth transitions. Hope below example helps (click red-bordered buttons to see the transition animation):

document.querySelector('.condition-list').addEventListener('click', function(){this.classList.toggle('edit--mode--off')});
.condition-element-type {
  display: inline-block;
  max-width: 71px;
  margin: 2px 10px 0 0;
}

.condition-element-names {
  display: inline-block;
  transition: transform .4s ease-out;
}

.condition-element-name {
  display: inline-block;
  margin-left: 10px;
  border-radius: 8px;
  color: $color-white;
  font-size: 11px;
  margin: 0 8px 8px 0;
  height: calc(1em + 5px);
  padding: 4px 8px 2px 8px;
  margin: -3px 8px 8px 0;
  
  border: 1px solid red;
  cursor: pointer;
}

.edit--mode--off .condition-element-names {
  transform: translate(-100px, 40px);
  margin-top: 5px;
}
<ul class="condition-list edit--mode--off">
  <!-- edit--mode--off is a dynamic class-->
  <li class="condition-element condition-element-selected ">
    <div class="condition-element-type">Primary EMF</div>
    <div class="condition-element-names">
      <div class="condition-element-name condition-growth">Innovation</div>
      <div class="condition-element-name condition-achievement">Mastery</div>
    </div>
  </li>
</ul>