萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS Transform 旋轉 Div:標準語法與 IE Matrix 濾鏡回退

現代標準寫法 (IE9+)

.rotated {
  transform: rotate(40deg);
  transform-origin: center center; /* 預設中心點,可改為 left top 等 */
}

歷史瀏覽器前綴 (構建工具自動處理)

.rotated {
  -webkit-transform: rotate(40deg);
  -moz-transform: rotate(40deg);
  -ms-transform: rotate(40deg);
  -o-transform: rotate(40deg);
  transform: rotate(40deg);
}

IE8 及以下:Matrix 濾鏡

.rotated {
  /* 40 度對應矩陣值 */
  filter: progid:DXImageTransform.Microsoft.Matrix(
    sizingMethod='auto expand',
    M11=0.76604444, M12=-0.64278761,
    M21=0.64278761, M22=0.76604444
  );
  /* 需額外處理定位偏移,極其麻煩 */
}

矩陣計算:`M11=cosθ, M12=-sinθ, M21=sinθ, M22=cosθ`。`sizingMethod='auto expand'` 自動擴大容器容納旋轉後內容。

完整範例 (含 IE 條件註釋定位修正)

<style>
#example {
  position: absolute; width: 500px; height: 400px;
  border: 1px solid #09f; padding: 10px; font-weight: bold;
  transform: rotate(40deg);
  -webkit-transform: rotate(40deg);
  -moz-transform: rotate(40deg);
  -o-transform: rotate(40deg);
  filter: progid:DXImageTransform.Microsoft.Matrix(
    sizingMethod='auto expand',
    M11=0.76604444, M12=-0.64278761,
    M21=0.64278761, M22=0.76604444
  );
}
</style>
<!--[if IE]>
<style>
#example { top: 50px; left: 50px; } /* IE 需手動修正位置 */
</style>
<![endif]-->

現代建議

  • **放棄 IE8 支援**,僅保留標準 `transform`。
  • 動畫旋轉用 `transition: transform 0.3s;` 或 `@keyframes spin { to { transform: rotate(360deg); } }`。
  • 3D 旋轉:`transform: rotateY(45deg);` 需父層 `transform-style: preserve-3d;` 與 `perspective`。
  • GPU 加速:`will-change: transform;` 提前建立合成層。
copyright © 萬盛學電腦網 all rights reserved