CSS3 transform-origin 属性
设置旋转元素的基点位置:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> #div1 { position: relative; height: 200px; width: 200px; margin: 100px; padding: 10px; border: 1px solid black; } #div2 { padding: 50px; position: absolute; border: 1px solid black; background-color: red; transform: rotate(45deg); transform-origin: 20% 40%; -ms-transform: rotate(45deg); /* IE 9 */ -ms-transform-origin: 20% 40%; /* IE 9 */ -webkit-transform: rotate(45deg); /* Safari and Chrome */ -webkit-transform-origin: 20% 40%; /* Safari and Chrome */ } </style> </head> <body> <div id="div1"> <div id="div2">HELLO</div> </div> </body> </html>
浏览器支持
表格中的数字表示支持该属性的第一个浏览器版本号。
紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号。
属性 | |||||
---|---|---|---|---|---|
transform-origin (two-value syntax) |
36.0 4.0 -webkit- |
10.0 9.0 -ms- |
16.0 3.5 -moz- |
9.0 3.2 -webkit- |
23.0 15.0 -webkit- 10.5 -o- |
transform-origin (three-value syntax) |
36.0 12.0 -webkit- |
10.0 | 16.0 10.0 -moz- |
9.0 4.0 -webkit- |
23.0 15.0 -webkit- |
属性定义及使用说明
transform-Origin属性允许您更改转换元素的位置。
2D转换元素可以改变元素的X和Y轴。 3D转换元素,还可以更改元素的Z轴。
为了更好地理解Transform-Origin属性,请查看这个演示.
<!DOCTYPE html> <html> <head> <style> #div1 { position: relative; height: 200px; width: 200px; margin: 50px; padding: 10px; border: 1px solid black; } #div2 { padding: 50px; position: absolute; border: 1px solid black; background-color: red; transform: rotate(45deg); transform-origin: 20% 40%; -ms-transform: rotate(45deg); /* IE 9 */ -ms-transform-origin: 20% 40%; /* IE 9 */ -webkit-transform: rotate(45deg); /* Safari and Chrome */ -webkit-transform-origin: 20% 40%; /* Safari and Chrome */ -moz-transform: rotate(45deg); /* Firefox */ -moz-transform-origin: 20% 40%; /* Firefox */ -o-transform: rotate(45deg); /* Opera */ -o-transform-origin: 20% 40%; /* Opera */ } </style> <script> function changeRot(value) { document.getElementById('div2').style.transform = "rotate(" + value + "deg)"; document.getElementById('div2').style.msTransform = "rotate(" + value + "deg)"; document.getElementById('div2').style.webkitTransform = "rotate(" + value + "deg)"; document.getElementById('div2').style.MozTransform = "rotate(" + value + "deg)"; document.getElementById('div2').style.OTransform = "rotate(" + value + "deg)"; document.getElementById('persp').innerHTML = value + "deg"; } function changeOrg() { var x = document.getElementById('ox').value; var y = document.getElementById('oy').value; document.getElementById('div2').style.transformOrigin = x + '% ' + y + '%'; document.getElementById('div2').style.msTransformOrigin = x + '% ' + y + '%'; document.getElementById('div2').style.webkitTransformOrigin = x + '% ' + y + '%'; document.getElementById('div2').style.MozTransformOrigin = x + '% ' + y + '%'; document.getElementById('div2').style.OTransformOrigin = x + '% ' + y + '%'; document.getElementById('origin').innerHTML = x + "% " + y + "%"; } </script> </head> <body> <p>Rotate the red div element, try changing its X-axis and Y-axis:</p> <div id="div1"> <div id="div2">HELLO</div> </div> Rotate: <input type="range" min="-360" max="360" value="45" onchange="changeRot(this.value)" /> transform: rotateY:(<span id="persp">45deg</span>); <br><br> X-axis: <input type="range" min="-100" max="200" value="20" onchange="changeOrg()" id="ox" /><br> Y-axis: <input type="range" min="-100" max="200" value="40" onchange="changeOrg()" id="oy" /> transform-origin: <span id="origin">20% 40%</span>; </body> </html>
注意: 使用此属性必须先使用 transform 属性。
Tip:Safari/Chrome用户:为了更好地理解3D 转换属性,请查看 - 演示.
<!DOCTYPE html> <html> <head> <style> #div1 { position: relative; height: 200px; width: 200px; margin: 50px; padding: 10px; border: 1px solid black; } #div2 { padding: 50px; position: absolute; border: 1px solid black; background-color: red; transform: rotateY(70deg); transform-origin: 50% 50% 100px; -webkit-transform: rotateY(70deg); /* Safari and Chrome */ -webkit-transform-origin: 50% 50% 100px; /* Safari and Chrome */ } </style> <script> function changeRot(value) { document.getElementById('div2').style.transform = "rotateY(" + value + "deg)"; document.getElementById('div2').style.webkitTransform = "rotateY(" + value + "deg)"; document.getElementById('div2').style.MozTransform = "rotateY(" + value + "deg)"; document.getElementById('div2').style.OTransform = "rotateY(" + value + "deg)"; document.getElementById('persp').innerHTML = value + "deg"; } function changeOrg() { var x = document.getElementById('ox').value; var y = document.getElementById('oy').value; var z = document.getElementById('oz').value; document.getElementById('div2').style.transformOrigin = x + '% ' + y + '% ' + z + 'px'; document.getElementById('div2').style.webkitTransformOrigin = x + '% ' + y + '% ' + z + 'px'; document.getElementById('div2').style.MozTransformOrigin = x + '% ' + y + '% ' + z + 'px'; document.getElementById('div2').style.OTransformOrigin = x + '% ' + y + '% ' + z + 'px'; document.getElementById('origin').innerHTML = x + "% " + y + "% " + z + "px"; } </script> </head> <body> <p><b>Note:</b> Internet Explorer 9 and earlier do not support 3D-transforms.</p> <p>Rotate the red div element, try changing its X-, Y-, and Z-axis:</p> <div id="div1"> <div id="div2">HELLO</div> </div> Rotate:<br> <input type="range" min="-360" max="360" value="70" onchange="changeRot(this.value)" /><br> -webkit-transform: rotateY:(<span id="persp">70deg</span>); <br> X-axis: <input type="range" min="-100" max="200" value="50" onchange="changeOrg()" id="ox" /><br> Y-axis: <input type="range" min="-100" max="200" value="50" onchange="changeOrg()" id="oy" /><br> Z-axis: <input type="range" min="-100" max="200" value="100" onchange="changeOrg()" id="oz" /><br> -webkit-transform-origin: <span id="origin">50% 50% 100px</span>; </body> </html>
默认值: | 50% 50% 0 |
---|---|
继承: | no |
版本: | CSS3 |
JavaScript 语法: | object.style.transformOrigin="20% 40%" |
语法
transform-origin: x-axis y-axis z-axis;
值 | 描述 |
---|---|
x-axis |
定义视图被置于 X 轴的何处。可能的值:
|
y-axis |
定义视图被置于 Y 轴的何处。可能的值:
|
z-axis |
定义视图被置于 Z 轴的何处。可能的值:
|