1. Basic knowledge:
2D transformation can change the position, size and shape attributes of the specified element.
To implement 2D conversion, you need to use the transform attribute. The attribute values are some conversion functions. They are introduced below.
2. Conversion function:
1.translate() function:
This function can be based on parameters Implement the movement effect of the specified element.
has two parameters, the first parameter specifies the displacement distance in the horizontal direction, and the second parameter specifies the displacement distance in the vertical direction.
Code example:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http:/ /" /> <title>php中文网</title> <style type="text/css"> #box{ width:400px; height:400px; background:green; } #box .demo{ width:50px; height:50px; background:red; transform:translate(50px,50px); -ms-transform:translate(50px,50px);/*IE9 */ -webkit-transform:translate(50px,50px);/*Safari and Chrome*/ -o-transform:translate(50px,50px);/* Opera*/ -moz-transform:translate(50px,50px);/* Firefox */ } </style> </head> <body> <div id="box"> <div class="demo"></div> </div> </body> </html>
The above code can set the displacement of the div in both the horizontal and vertical directions to 50px.
Special Note: If there is only one parameter, then this parameter applies to both horizontal and vertical orientations; if the parameter is a percentage value, the reference size is the corresponding length and width size of the element itself; the same applies to the following functions.
2.rotate() function:
This function can specify the clockwise rotation angle of the element. If it is a negative value, it is counterclockwise rotation.
Code example:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> #box{ width:400px; height:400px; background:green; } #box .demo{ width:50px; height:50px; background:red; transform:rotate(60deg); -ms-transform:rotate(60deg);/* IE 9 */ -webkit-transform:rotate(60deg);/* Safari and Chrome */ -o-transform:rotate(60deg);/* Opera */ -moz-transform:rotate(60deg); } </style> </head> <body> <div id="box"> <div class="demo"></div> </div> </body> </html>
The above code can rotate the div element 60 degrees clockwise.
3.scale() function:
This function can achieve the scaling effect of the specified element.
has two parameters, the first parameter is the width scaling factor, and the second is the height scaling factor.
Code example:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> #box{ width:400px; height:400px; background:green; margin:100px; } #box .demo{ width:50px; height:50px; background:red; transform:scale(2,3); -ms-transform:scale(2,3);/* IE 9 */ -webkit-transform:scale(2,3);/* Safari 和 Chrome */ -o-transform:scale(2,3);/* Opera */ -moz-transform:scale(2,3);/* Firefox */ } </style> </head> <body> <div id="box"> <div class="demo"></div> </div> </body> </html>
The above code can double the width of the div and triple the height.
4.skew() function:
This function can set the distortion angle of the specified element.
has two parameters, the first parameter sets the distortion in the horizontal direction, and the second parameter sets the distortion in the vertical direction.
If the second parameter is omitted, the default value is 0.
Code Example 1:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> #box{ position:relative; height:200px; width:200px; margin-top:150px; margin-left:150px; border:1px solid black; } #inner{ padding:50px; position:absolute; border:1px solid black; background-color:yellow; transform-origin:40 40; font-size:12px; transform:skew(45deg); -ms-transform:skew(45deg);/*IE9*/ -webkit-transform:skew(45deg);/*Safari and Chrome*/ -o-transform:skew(45deg);/*Opera*/ -moz-transform:skew(45deg);/*Firefox*/ } table{ font-size:12px; width:300px; margin-left:120px; } .left{text-align:right} </style> <script type="text/javascript"> function changeRot(value){ var oinner=document.getElementById('inner'); var opersp=document.getElementById('persp'); oinner.style.transform="skew(" + value + "deg)"; oinner.style.msTransform="skew(" + value + "deg)"; oinner.style.webkitTransform="skew(" + value + "deg)"; oinner.style.MozTransform="skew(" + value + "deg)"; oinner.style.OTransform="skew(" + value + "deg)"; opersp.innerHTML=value + "deg"; } window.onload=function(){ var range=document.getElementById("range"); range.onmousemove=function(){ changeRot(this.value); } } </script> </head> <body> <div id="box"> <div id="inner">php中文网</div> </div> <table> <tr> <td class="left">扭曲:</td> <td><input type="range" min="-360" max="360" id="range" value="45"/></td> </tr> <tr> <td class="left">skew:</td> <td>(<span id="persp">45deg</span>)</td> </tr> </table> </body> </html>
The above code can test the distortion in the horizontal direction. Drag the scroll bar to test, and you can see it clearly at a glance.
Example code two:
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://ask.php.cn/" /> <title>php中文网</title> <style type="text/css"> #box{ position:relative; height:200px; width:200px; margin-top:150px; margin-left:150px; border:1px solid black; } #inner{ padding:50px; position:absolute; border:1px solid black; background-color:yellow; transform-origin:40 40; font-size:12px; transform:skew(0,45deg); -ms-transform:skew(0,45deg);/*IE9*/ -webkit-transform:skew(0,45deg);/*Safari and Chrome*/ -o-transform:skew(0,45deg);/*Opera*/ -moz-transform:skew(0,45deg);/*Firefox*/ } table{ font-size:12px; width:300px; margin-left:120px; } .left{text-align:right} </style> <script type="text/javascript"> function changeRot(value){ var oinner=document.getElementById('inner'); var opersp=document.getElementById('persp'); oinner.style.transform="skew(0," + value + "deg)"; oinner.style.msTransform="skew(0," + value + "deg)"; oinner.style.webkitTransform="skew(0," + value + "deg)"; oinner.style.MozTransform="skew(0," + value + "deg)"; oinner.style.OTransform="skew(0," + value + "deg)"; opersp.innerHTML=value + "deg"; } window.onload=function(){ var range=document.getElementById("range"); range.onmousemove=function(){ changeRot(this.value); } } </script> </head> <body> <div id="box"> <div id="inner">php中文网</div> </div> <table> <tr> <td class="left">扭曲:</td> <td><input type="range" min="-360" max="360" id="range" value="45"/></td> </tr> <tr> <td class="left">skew:</td> <td>(<span id="persp">45deg</span>)</td> </tr> </table> </body> </html>
The above code can test the distortion in the vertical direction. Drag the scroll bar to test, and you can see it clearly at a glance.
5.matrix() function:
Specify a transformation matrix in the form of a six-value (a, b, c, d, e, f) 2D transformation is equivalent to directly applying a [a b c d e f] transformation matrix.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网</title> <style> div { width:100px; height:75px; background-color:red; border:1px solid black; } div#div2 { transform:matrix(0.866,0.5,-0.5,0.866,0,0); -ms-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* IE 9 */ -webkit-transform:matrix(0.866,0.5,-0.5,0.866,0,0); /* Safari and Chrome */ } </style> </head> <body> <div>Hello. This is a DIV element.</div> <div id="div2">Hello. This is a DIV element.</div> </body> </html>
Use the matrix() method to rotate the div element 30°
Next Section