skip to Main Content

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>

2

Answers


  1. const circle = document.getElementById('circle');
    let deg = 0;
    
    function rotate() {
      deg += 60;
      circle.style.setProperty('--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;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%) rotate(calc(var(--i) * 60deg)) translateY(150px);
      /* Position around circle */
      width: 30px;
    }
    
    .btn {
      align-items: center;
      background: lawngreen;
      display: flex;
      height: 30px;
      justify-content: center;
      width: 100px;
      cursor: pointer;
      margin: 20px auto;
    }
    <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>
    Login or Signup to reply.
  2. 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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search