Home >Web Front-end >HTML Tutorial >CSS3 Metamorphosis_html/css_WEB-ITnose
CSS3 transformation
CSS3 transformation is a collection of effects, such as translation, rotation, scaling and tilt effects. Each effect is called a transformation function.
The transform attribute applies a 2D or 3D transformation to an element. This property allows us to rotate, scale, move or tilt the element.
transform:none | <transform-function> [<transform-function>] *
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> div{ width:100px; height:100px; margin:40px; position:absolute; opacity:1; background:rgb(0, 148, 255); } .box2{ background:rgb(0, 148, 255); opacity:.5; /*旋转45度*/ transform:rotate(45deg); }</style></head><body><div class="box1"></div> <div class="box2"></div> </body></html>
Rotation
Scale
.box2{ ... /*放大了1.5倍*/ transform:scale(1.5);}
Displacement
.box2{ ... /*X轴与Y轴都移动150px*/ transform:translate(150px,150px);}
skew
.box2{ ... /*X轴倾斜30度*/ transform:skew(30deg);}
Used to specify the center point position of the element.
transform-origin:x-axis y-axis z-axis
When transform transforms, by default, the center point of the element is used as the origin. The center point of the element can be changed through the transform-origin attribute, so that the point we specify is used as the center point for transformation.
.box2{ ... transform-origin:left top;}
In the above example, the center point of the element is changed from the default middle point center to the upper left corner point left top. That is to say, the point in the upper left corner is used as the base point for deformation. It's very simple. I won't demonstrate the points at other positions one by one. In addition, transform-origin cannot change the midpoint of translate.
The transform-style attribute specifies how nested elements are rendered in 3D space.
transform-style:flat | preserver-3d
To put it simply, it is able to create a 3D space. Let child elements deform in 3D space.
The perspective attribute is used to set the distance between the user and the Z plane of the element's 3D space. The smaller the value, the more obvious the 3D effect is.
perspective:none | number
When an element defines the perspective attribute, its child elements will get the perspective effect, not the element itself. If you don't specify a perspective, all points in Z-space will tile into the same 2D plane.
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> /*没有添加perspective时效果*/ #box1 div{ position:absolute; } #box1 div img{ transform-origin:bottom; } #box1 .box1-1 img{ opacity:.5; } #box1 .box1-2 img{ transform:rotateX(45deg); } /*添加perspective时效果*/ #box2 div{ position:absolute; left:400px; perspective:500px; } #box2 div img{ transform-origin:bottom; } #box2 .box2-1 img{ opacity:.5; } #box2 .box2-2 img{ transform:rotateX(45deg); }</style></head><body><div id="box1"> <div class="box1-1"><img src="https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png"></div> <div class="box1-2"><img src="https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png"></div></div><div id="box2"> <div class="box2-1"><img src="https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png"></div> <div class="box2-2"><img src="https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png"></div></div></body></html>
It can be seen that the 3D effect is more obvious when the perspective element is set.
In addition, in 3D deformation, in addition to the perspective attribute that can activate a 3D space, the perspective() in the 3D deformation function can also activate the 3D space. The difference is that the perspective attribute is used in the parent element, and the perspective() function is used in the current child element and is used together with other functions in transform. For example:
transform:rotate(45deg) perspective(500px);
The effect is the same.
It is used to determine the starting point of the perspective attribute, which is simply the viewing angle.
perspective-origin: x-axis y-axis;
Perspective-origin, like the perspective attribute, must be defined on the element of the parent element. In other words, perspective-origin is used in conjunction with perspective.
#box2 div{ ... perspective-origin:bottom right;}
The effect in the lower right corner
The effect in the lower left corner
The effect in the upper left corner
Upper right corner effect
Upper right corner effect
Right corner effect
Lower corner effect
Left corner effect
Default value, middle corner.
The backface-visibility attribute defines whether the element is visible when it is not facing the screen. This property is useful if you rotate an element and don't want to see the back side.
backface-visibility:visible | hidden;
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @keyframes rotate{ 0%{ transform:rotateY(0deg); } 100%{ transform:rotateY(360deg); } } #box1,#box2{ width:300px; height:400px; float:left; margin:0 20px; transform-style: preserve-3d; animation:rotate 3s ease-in-out infinite alternate; } div div { perspective: 1000px; position:absolute; top:0; right:0; bottom:0; left:0; } #box1 div{ /*第一个图片背面不可见*/ backface-visibility: hidden; } #box1 .box1-1,#box2 .box2-1{ background:url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png)no-repeat 0 0 / 100% 100%; z-index:2; } #box1 .box1-2,#box2 .box2-2{ background:url(https://img.alicdn.com/imgextra/i2/2406102577/TB2cgJ7dVXXXXb3XXXXXXXXXXXX_!!2406102577.png)no-repeat 0 0 / 100% 100%; transform:rotateY(180deg); }</style></head><body><div id="box1"> <div class="box1-1"></div> <div class="box1-2"></div></div><div id="box2"> <div class="box2-1"></div> <div class="box2-2"></div></div></body></html>
When the two pictures are on the front, there is no change.
When the two pictures are on the back, the back side is set. Visible image hides the front.
Copy the code and view it in the browser to understand it better.
The translate() function can move elements from their original positions.
translate(x-axis,y-axis);
The translate() function can take one value x-axis, or two values x-axis, y-axis.
When taking a value, x-axis represents the distance moved by the X-axis. When the value is positive, the element moves to the right, and when the value is negative, the element moves to the left.
When taking two values, the x-axis is the same; y-axis represents the Y axis. When the value is positive, the element moves downward, and when the value is negative, the element moves upward.
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @-webkit-keyframes translate{ from{ transform:translate(0); } 20%{ transform:translate(100px); } 40%{ transform:translate(100px,100px); } 60%{ transform:translate(200px,100px); } 80%{ transform:translate(200px,200px); } 100%{ transform:translate(300px,200px); } } @keyframes translate{ from{ transform:translate(0); } 20%{ transform:translate(100px); } 40%{ transform:translate(100px,100px); } 60%{ transform:translate(200px,100px); } 80%{ transform:translate(200px,200px); } 100%{ transform:translate(300px,200px); } } div{ width:100px; height:100px; background:hsl(102, 100%, 50%); -webkit-animation:translate 1s ease infinite alternate; animation:translate 1s ease infinite alternate; }</style></head><body> <div></div></body></html>
The following is the displacement effect of the two pictures. It is recommended to copy the code and view it in the browser.
If you want to move the object along the X-axis or Y-axis, you can use translate(x-axis,0) and translate(0 ,y-axis) to achieve. You can also use the translateX() and translateY() functions.
translateX(100px)等于translate(100px,0)translateY(100px)等于translate(0,100px)
The scaling function scale() allows the element to scale based on the central origin.
scale(x-axis,y-axis);
The scale() function has similar syntax to the translate() function. It can accept one value or two values. When there is only one value, the second value is the same as the first value. That is to say, the X-axis scales proportionally with the Y-axis. The default value is 1. When the value is less than 1, the element shrinks; when the value is greater than 1, the element enlarges.
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @-webkit-keyframes scale{ from{ transform:scale(-3); } 100%{ transform:scale(5); } } @keyframes scale{ from{ transform:scale(-3); } 100%{ transform:scale(5); } } div{ position:absolute; left:40%; top:40%; width:70px; height:100px; background:url(https://img.alicdn.com/imgextra/i2/2406102577/TB24namdVXXXXbXXXXXXXXXXXXX_!!2406102577.png) no-repeat scroll 0 0 / 100% 100%; -webkit-animation:scale 1s ease infinite alternate; animation:scale 3s ease infinite alternate; }</style></head><body><div></div></body></html>
In addition to taking positive values, the scale() function can also take negative values. A negative value will cause the element to be flipped before scaling. Below are two animation clips that can be copied and viewed in the browser.
除了能通过scale()函数使元素在X轴和Y轴进行缩放之外,还可以使用scaleX()与scaleY()两个函数分别在X轴与Y轴缩放。
scaleX(2)相当于scale(2,1)scaleY(2)相当于scale(1,2)
另外,缩放函数默认是以中心点为原点进行变形,可以使用transform-origin函数,修改元素的中心点,使缩放函数的效果不同。
倾斜函数skew()能够让元素倾斜显示。与rotate()函数不同,rotate()函数只能旋转,不能改变元素形状;skew()函数不能旋转,当会改变元素形状。
skew(x-axis,y-axis)
x-axis表示元素X轴倾斜的角度。
y-axis表示元素Y轴倾斜的角度。
与前几个函数一样,当只有一个值时,表示的是X轴进行倾斜;二个值时,X轴与Y轴同时倾斜。
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> div { width: 100px; height: 100px; background: hsl(102, 100%, 50%); transform:skew(30deg); }</style></head><body><div></div></body></html>
元素X轴倾斜效果
div { ... transform:skew(0,30deg);}
元素Y轴倾斜效果
div { ... transform:skew(30deg,30deg);}
元素X轴与Y轴同时倾斜效果
除了使用scale()函数使X轴与Y轴倾斜之外,还可以使用scaleX()与scaleY()函数让元素在X轴与Y轴倾斜。
scaleX(30deg)等于scale(30deg,0)scaleY(30deg)等于scale(0,30deg)
还可以使用transform-origin属性修改元素变形的中心点,让元素有不同的倾斜效果。
俺正在学习当中。点我我也要去学。
CSS3中3D位移主要包括两种函数translateZ()和translate3d()。
translateZ(z-axis);
功能是让元素在3D空间沿Z轴进行位移。负值时,元素在视觉上感觉越来越远,导致元素变小;正值时,元素在视觉上感觉越来越近,导致元素变大了。代码效果translate3d()函数中。
translate3d(x-axis,y-axis,z-axis);
translate3d()函数使一个元素在三维空间移动。是translateX(),translateY(),translateZ()三个函数的缩写,与translate()函数不同的是,translate()函数只能作用在X轴与Y轴上,也就是在二维平面上移动。
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @-webkit-keyframes translate3d { 0% { transform: translate3d(-30px,-20px,-300px); } 100% { transform: translate3d(30px,20px,200px); } } @keyframes translate3d { 0% { transform: translate3d(-30px,-20px,-300px); } 100% { transform: translate3d(30px,20px,200px); } } body { transform-style: preserve-3d; perspective: 1000px; } .box1 { position:absolute; width: 70px; height: 100px; background: url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat 0 0 / 100% 100%; -webkit-animation: translate3d 2s ease-in-out infinite alternate; animation: translate3d 2s ease-in-out infinite alternate; position:absolute; top:30px; left:20%; } .box2{ background: url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat 0 0 / 100% 100%; opacity:.5; width: 70px; height: 100px; position:absolute; top:30px; left:20%; }</style></head><body><div class="box1"></div><div class="box2"></div></body></html>
下面是两张动画片段,图中可以看出Z轴的移动对视觉上的效果。
CSS3 3D变形中的缩放主要有scaleZ()和scale3d()两种函数。
scaleZ(z-axis);
让元素在Z轴上按比例缩放。
scale3d(x-axis,y-axis,z-axis);
是scaleX,scaleY,scaleZ三个函数的缩写,scaleZ(2)等于scale3d(1,1,2)。与scale函数不同的是,scale()是在二维平面上进行缩放。scale3d()在三维空间上进行缩放。scale3d()与scaleZ()要与其他函数一起使用才有效果。
建议在使用transform调用函数时,scale3d()或scaleZ()要写在其他函数之前,不然也没有效果。
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @keyframes scale3d{ 0%{ transform:scale3d(1,1,-10) rotateX(0); } 100%{ transform:scale3d(1,1,2) rotateX(40deg); } } html,body{ transform-style: preserve-3d; perspective: 1200px; } div{ width: 70px; height: 100px; position:absolute; } .box1{ left:40%; top:40px; background:url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat scroll 0 0 /100% 100%; z-index:2; animation:scale3d 1s ease-in-out infinite alternate; } .box2{ left:40%; top:40px; opacity:.5; background:url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat scroll 0 0 /100% 100%; }</style></head><body><div class="box1"></div><div class="box2"></div></body></html>
下面是动画片段
CSS3中3D旋转可以使用rotateX(),rotateY(),rotateZ()三个函数分别设置三维空间的旋转。也可以使用rotate3d()函数集中进行设置,因为效果都是一样的,俺就选rotate3d()函数来说。
rotate3d(x,y,z,deg);
x,y,z代表旋转的三个轴,取值是0或1,0时代表这个轴不旋转,1时代表这个轴旋转。deg代表旋转的角度。
rotateX(45deg)等于rotate3d(1,0,0,45deg)rotateY(45deg)等于rotate3d(0,1,0,45deg)rotateZ(45deg)等于rotate3d(0,0,1,45deg)rotateX(45deg);rotateY(45deg);等于rotate3d(1,1,0,45deg)...
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><meta charset="utf-8" /><title></title><style> @keyframes rotate3d{ 0%{ transform:rotate3d(0,0,0,0); } 100%{ transform:rotate3d(1,1,1,45deg); } } html,body{ transform-style: preserve-3d; perspective: 1200px; } div{ width: 70px; height: 100px; position:absolute; } .box1{ left:40%; top:40px; background:url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat scroll 0 0 /100% 100%; z-index:2; animation:rotate3d 1s ease-in-out infinite alternate; } .box2{ left:40%; top:40px; opacity:.5; background:url(https://img.alicdn.com/imgextra/i4/2406102577/TB28gKadVXXXXbmXXXXXXXXXXXX_!!2406102577.png) no-repeat scroll 0 0 /100% 100%; }</style></head><body><div class="box1"></div><div class="box2"></div></body></html>
下面是动画片段
学习中...
CSS3 变形就写到此了。
总结一下最近写博客的感想。以前不写博客时,一本书几天就能看完,而且自己又光看不练,且又不爱复习,那过几天立马就忘得干净了。多次吃亏之下,决定写博客,做笔记。写博客的这几天,一个字“累”,以往看书时感觉不过尔尔,简单的很,等到自己写,就糟了,感觉左也不对,右也是错,这时才知道难啊。写出来后欢心鼓舞,好不快乐。写博客累,却是实在。子曰:学而不思则罔。唯有自己想出来,写下来,才不会迷惑。乡下人言语粗鄙,不知所云。