Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to prevent child from rotating with parent during the transition?

I’m trying to rotate a parent circle and negate the effect on its children.
I tried to negate the effect by using

transform: translate(-50%, -50%)
           rotate(calc(var(--i) * -60deg))
           translateY(150px)
           rotate(calc(var(--i) * 60deg - var(--rotation)))

but the children will rotate 60deg at the start of the transition. How to prevent them from rotating completely duration the transition?

const circle = document.getElementById('circle');
let deg = 0;

function rotate() {
  deg += 60;
  circle.style.cssText = `--rotation: ${deg}deg`;
}
.circle {
  border: 2px solid #ccc;
  border-radius: 50%;
  height: 300px;
  margin: 40px auto 0;
  transform: rotate(var(--rotation));
  transition: transform 1000ms ease;
  position: relative;
  width: 300px;
}

.item {
  align-items: center;
  background: red;
  border: 2px solid #fff;
  border-radius: 50%;
  display: flex;
  height: 30px;
  justify-content: center;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%) rotate(calc(var(--i) * -60deg)) translateY(150px) rotate(calc(var(--i) * 60deg - var(--rotation)));
  width: 30px;
}

.btn {
  align-items: center;
  background: lawngreen;
  display: flex;
  height: 30px;
  justify-content: center;
  width: 100px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<div class="circle" id="circle" style="--rotation: 0deg">
  <div class="item" style="--i: 0">1</div>
  <div class="item" style="--i: 1">2</div>
  <div class="item" style="--i: 2">3</div>
  <div class="item" style="--i: 3">4</div>
  <div class="item" style="--i: 4">5</div>
  <div class="item" style="--i: 5">6</div>
</div>

<div class='btn' onclick="rotate()">rotate</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are almost there, simply add the same transition to the child elements

const circle = document.getElementById('circle');
let deg = 0;

function rotate() {
  deg += 60;
  circle.style.cssText = `--rotation: ${deg}deg`;
}
.circle {
  border: 2px solid #ccc;
  border-radius: 50%;
  height: 300px;
  margin: 40px auto 0;
  transform: rotate(var(--rotation));
  transition: transform 1000ms ease;
  position: relative;
  width: 300px;
}

.item {
  align-items: center;
  background: red;
  border: 2px solid #fff;
  border-radius: 50%;
  display: flex;
  height: 30px;
  justify-content: center;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%) rotate(calc(var(--i) * -60deg)) translateY(150px) rotate(calc(var(--i) * 60deg - var(--rotation)));
  transition: transform 1000ms ease;
  width: 30px;
}

.btn {
  align-items: center;
  background: lawngreen;
  display: flex;
  height: 30px;
  justify-content: center;
  width: 100px;
}
<div class="circle" id="circle" style="--rotation: 0deg">
  <div class="item" style="--i: 0">1</div>
  <div class="item" style="--i: 1">2</div>
  <div class="item" style="--i: 2">3</div>
  <div class="item" style="--i: 3">4</div>
  <div class="item" style="--i: 4">5</div>
  <div class="item" style="--i: 5">6</div>
</div>

<div class='btn' onclick="rotate()">rotate</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading