如何防止孩子在过渡过程中与家长轮流?

问题描述 投票:0回答:1

我正在尝试旋转父圆并消除对其子圆的影响。 我试图通过使用来抵消效果

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

但是孩子们将在过渡开始时旋转 60 度。如何防止它们在过渡期间完全旋转?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .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;
    }
  </style>
</head>

<body>
  <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>

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

    function rotate() {
      deg += 60;
      circle.style.cssText = `--rotation: ${deg}deg`;
    }
  </script>
</body>

</html>

javascript html css
1个回答
0
投票
  <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        .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;
        }
      </style>
    </head>
    <body>
      <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>
      <script>
        const circle = document.getElementById('circle');
        let deg = 0;
    
        function rotate() {
          deg += 60;
          circle.style.setProperty('--rotation', `${deg}deg`);
        }
      </script>
    </body>
    </html>
© www.soinside.com 2019 - 2024. All rights reserved.