Tramsform旋转

Tramsform的小技巧

by Robbie Yang

HTML

<body class="flex">
    <div class="container">
        <div class="item"></div>
    </div>
</body>

CSS

html,body {
  padding: 0;
  margin: 0;
}

.flex {
  /** 使用vh单位进行布满全屏 */
  min-height: 100vh; 
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  position: relative;
  width: 500px;
  height: 500px;
  background-color: #ccc;   
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  width: 100px;
  height: 100px;
  background-color: #000;
  transform-origin: left top;
  animation: scaleAnim 1.5s ease-out infinite;
}

.container::before {
  position: absolute;
  top: 195px;
  left: 195px;
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 10px;
  content: '';
  background-color: #fff;
  z-index: 10;
}

@keyframes scaleAnim {
  0% {
    transform: matrix(1,0,0,1,0,0);
  }

  50% {
    /** 旋转matrix(cosθ,sinθ,-sinθ,cosθ,0,0) */
    transform: matrix(0.866025,0.500000,-0.500000,0.866025,0,0);
  }

  100%  {
    transform: matrix(1,0,0,1,0,0);
  }
}