表单不绕中心轴(x,y)旋转

时间:2019-01-03 10:06:01

标签: css

即使我在CSS中设置了[transform-origin:100%50%;],但与X轴和Y轴相比,该表单也不会围绕其中心旋转。

问题:如何使表格旋转准确地从其中点开始计数。

    .box {
      background-color: pink;
      margin: 300px 0 0 300px;
      width: 200px;
      height: 200px;
    }
    
    .box {
      position: absolute;
      animation: spin 10s;
      animation-fill-mode: forwards;
    }
    
    .line-horizontal {
      background-color: black;
      width: 200px;
      height: 5px;
      margin: 100px 0 0 0;
    }
    
    .line-vertical {
      background-color: black;
      width: 5px;
      height: 200px;
      margin: -105px 0 0 100px;
    }
    
    @keyframes spin {
      from {
        transform:rotate(0deg);
        transform-origin: 100% 50%;
      }
      to {
        transform:rotate(360deg);
      }
    }
    
    
    <div class="box">
      <div class="line-horizontal"></div>
      <div class="line-vertical"></div>
    </div>

1 个答案:

答案 0 :(得分:0)

tranform-origin更改为50% 50%,这意味着两个轴的中心都不要放在关键帧内,因为它会变得生动

.box {
  background-color: pink;
  width: 200px;
  height: 200px;
}

.box {
  position: absolute;
  animation: spin 10s;
  animation-fill-mode: forwards;
  transform-origin: 50% 50%;
  /*center*/
}

.line-horizontal {
  background-color: black;
  width: 200px;
  height: 5px;
  margin: 100px 0 0 0;
}

.line-vertical {
  background-color: black;
  width: 5px;
  height: 200px;
  margin: -105px 0 0 100px;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div class="box">
  <div class="line-horizontal"></div>
  <div class="line-vertical"></div>
</div>

如果您需要相同的外观,也可以像下面那样简化代码:

.box {
  width: 200px;
  height: 200px;
  background:
    linear-gradient(#000,#000) center/100% 5px,
    linear-gradient(#000,#000) center/5px 100%,
    pink;
  background-repeat:no-repeat;
  animation: spin 10s forwards;
  transform-origin: 50% 50%; /* OR center*/
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div class="box">
</div>