rotate() 讓元素繞中心原點旋轉。這裡談的是二維平面上的旋轉,不涉及 3D 空間的運作。
語法:
transform:rotate(度數);
說明:
度數指元素相對中心原點旋轉的角度,單位為 deg,也就是 degree 的縮寫。度數為正時順時針旋轉,為負則逆時針旋轉。
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>CSS3旋轉rotate()方法</title>
<style type="text/css">
/*設置原始元素樣式*/
#origin {
margin:100px auto;
/*水平居中*/
width:200px;
height:100px;
border:1px dashed gray;
}
/*設置當前元素樣式*/
#current {
width:200px;
height:100px;
line-height:100px;
color:white;
background-color: #007BEE;
text-align:center;
transform:rotate(30deg);
-webkit-transform:rotate(30deg);
/*兼容-webkit-引擎浏覽器*/
-moz-transform:rotate(30deg);
/*兼容-moz-引擎浏覽器*/
}
</style>
</head>
<body>
<div id="origin">
<div id="current">順時針旋轉30度</div>
</div>
</body>
</html>
分析:
虛線框是元素的原始位置,藍色方塊則是順時針旋轉 30 度之後的樣子。仔細看會發現旋轉後元素的尺寸沒有變,只是四角離開了原本的位置——這是因為旋轉預設繞正中央進行,而元素本身的寬高並未改變。
若想改以其他點為旋轉中心,可以搭配下一節會提到的 transform-origin。