web编程之路让前端飞web前端学习

CSS3 Transform——transform-origin

2016-10-14  本文已影响1971人  hopevow

关于css3变形

css3的相关函数:

transform

transform-origin属性

orignin changeorigin

正如上图所示,改变transform-origin属性的X轴和Y轴的值就可以重置元素变形原点位置,

 right = right center = center right = 100%或(100% 50%)
bottom = bottom center = center bottom = 50% 100%
 left = left center = center left = 0或(0 50%)
 center = center center = 50%或(50% 50%)
 top left = left top = 0 0
 right top = top right = 100% 0
 bottom right = right bottom = 100% 100%
 bottom left = left bottom = 0 100%```

>以transform中的旋转rotate()为例,并transform-origin取值不一样时的效果:

* transform-origin取值为center(或center center或50% 或50% 50%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/370c7f95aecd7fa8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为top(或top center或center top或50% 0):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/026b01e6f3a61a40.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* transform-origin取值为right(或right center 或center right 或 100% 或 100% 50%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/4989fe517e778d91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为bottom(或bottom center 或center bottom 或 50% 100%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/31828a6da638138d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为left(或left center或center left或0或0 50%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/51bdcdff2584f4aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为top left(或left top或0 0):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/7896b40f8ee1b086.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为right top(或top right或100% 0):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/91f4d31e84b0ffa0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* transform-origin取值为bottom right(或right bottom或100% 100%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/b63e23c15b4b7711.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* transform-origin取值为left bottom(或bottom left 或 0 100%):

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/4eee4db508d9bbf1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

>CSS3变形中旋转、缩放、倾斜都可以通过`transform-origin`属性重置元素的原点,但其中的位移`translate()`始终以元素中心点进行位移。例如下面的两段代码的演示过程:

```div { -webkit-transform-origin: 50% 50%; -moz-transform-origin: 50% 50%; -o-transform-origin: 50% 50%; -ms-transform-origin: 50% 50%; transform-origin: 50% 50%; -webkit-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -moz-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -o-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -ms-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px);}```

接下来通过transform-origin将变形原点设置为100% 100%:

```div { -webkit-transform-origin: 100% 100%; -moz-transform-origin: 100% 100%; -o-transform-origin: 100% 100%; -ms-transform-origin: 100% 100%; transform-origin: 100% 100%; -webkit-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -moz-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -o-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); -ms-transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px); transform: translate(40px, 40px) translate(-50px, 35px) translateY(30px);}```
虽然元素的变形原点通过 ` transform-origin`  从50% 50%变成100% 100%,但元素位移`translate()`始终是依元素中心点进行位移,如下图所示:

>到目前为止,transform-origin
属性在现代主流浏览器得到很好的支持,但在一些浏览器之下依然需要添加各浏览器私有属性,详细情况如下所示:

*  2D变形中transform-origin需要添加浏览器私有属性版本:
 * IE9+、Firefox3.5+、Chrome4+、Safari3.1+、Opera10.5+;iOS Safari3.2+、Android Browser2.1+、Blackberry Browser7.0+、Chrome for Android25.0+。
  * 2D变形中transform-origin支持W3C标准规范的浏览器:IE10+、Firefox16+、Opera12.1+;Opera Mobile11.0+、Firefox for Android19.0。
 * 3D变形中transform-origin需要添加浏览器私有属性版本:IE10+、Firefox10+、Chrome12+、Safari4+、Opera15+、iOS Safari3.2+、Android Browser3.0+、Blackberry Browser7.0+、Opera Mobile14.0+、Chrome for Android25.0+。
 *  3D变形中transform-origin支持W3C标准规范的浏览器:Firefox16+、Firefox for Android19+

>通过`transform-origin`属性改变元素的原点,可以实现不同的变形效果,下面的示例中我们分别演示了改变元素原点前后,CSS3变形各函数对图像变形操作。

* 为了能更具有对比性,下面的示例中有两个div,每个div各有5个img,而第一个div是指transform-origin为默认值时效果,第二个div是指transform-origin修改后在不同transform函数中效果。

```<div> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /></div><div> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /> <img src="images/cardKingClub.png" alt="" width="142" height="200" /></div>```

* 默认样式:

```div { width: 500px; height: 300px; margin: 30px auto; position: relative; background: url(images/bg-grid.jpg) no-repeat center center; background-size: 100% 100%;}div img { position: absolute; top: 50%; left: 50%; margin-left: -71px; margin-top: -100px; }```

* 首先来看transform-origin属性改变元素原点前后rotate()函数对图像的旋转效果:
```div img:nth-child(1){ opacity: .5; z-index: 1; transform: rotate(10deg);}div img:nth-child(2){ opacity: .6; z-index: 2; transform: rotate(25deg);}div img:nth-child(3){ opacity: .7; z-index: 3; transform: rotate(35deg);}div img:nth-child(4){ opacity: .8; z-index: 4; transform: rotate(45deg);}div img:nth-child(5){ z-index: 5; transform: rotate(60deg);}div:nth-of-type(2) img { transform-origin: bottom;}```
* 上面实例演示了变形中旋转rotate()函数围绕不同原点旋转的效果,第一个容器div中的图片围绕图片默认原点(中心)旋转过程;而第二个容器div中的图片经transform-origin属性将图片原点从中心点(center)修改为底部中心点(bottom)旋转过程:


![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/edddf52c5914132c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* 接下来,我们在来看transform-origin修改原点前后,CSS3变形中倾斜skew()函数对图片变形的过程:

```div img:nth-child(1){ opacity: .5; z-index: 1; transform: skewX(10deg);}div img:nth-child(2){ opacity: .6; z-index: 2; transform: skewX(15deg);}div img:nth-child(3){ opacity: .7; z-index: 3; transform: skewX(20deg);}div img:nth-child(4){ opacity: .8; z-index: 4; transform: skewX(25deg);}div img:nth-child(5){ z-index: 5; transform: skewX(30deg);}div:nth-of-type(2) img { transform-origin: bottom;}```

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/83b7d305c9f922f9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

* 上面两个例子简单的演示了变形中的rotate()和skew()函数都可以通过transform-origin属性改变对象原点,让对象根据不同的原点进行变形。接来我继续向大家演示变形中的缩放scale()函数在不同原点产生变形效果:
```div img:nth-child(1){ opacity: .5; z-index: 1; transform: scale(1.2);}div img:nth-child(2){ opacity: .6; z-index: 2; transform: scale(1.1);}div img:nth-child(3){ opacity: .7; z-index: 3; transform: scale(.9);}div img:nth-child(4){ opacity: .8; z-index: 4; transform: scale(.8);}div img:nth-child(5){ z-index: 5; transform: scale(.6);}div:nth-of-type(2) img { transform-origin: right;}```

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/d6c51310939b8cae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

*上面三个简单实例再次验证了CSS3变形中的旋转rotate()、缩放scale()和倾斜skew()函数都可以通过transform-origin属性来改变元素对象的原点位置。但是transform-origin属性改变元素对像原点位置,位移translate()函数始终会根据元素对像中心点进行位移。

*前面演示的只是2D变形中transform-origin用来修改元素对象原点,以及对各种变形函数产生的不同效果。接下来,在来看一个简单的实例,演示一下3D变形中transform-origin修改元素原点的3D旋转效果。
```div img { position: absolute; top: 50%; left: 50%; margin-left: -71px; margin-top: -100px; backface-visibility: visible; transform: perspective(500px);}div img:nth-child(1){ opacity: .5; z-index: 1; transform: rotate3d(1, 1, 1,10deg);}div img:nth-child(2){ opacity: .6; z-index: 2; transform: rotate3d(1, 1, 1,25deg);}div img:nth-child(3){ opacity: .7; z-index: 3; transform: rotate3d(1, 1, 1,35deg);}div img:nth-child(4){ opacity: .8; z-index: 4; transform: rotate3d(1, 1, 1,45deg);}div img:nth-child(5){ z-index: 5; transform: rotate3d(1, 1, 1,60deg);}div:nth-of-type(2) img { transform-origin: left bottom -50px;}```

![Paste_Image.png](http:https://img.haomeiwen.com/i3079704/9fd803f4cb4dd860.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

转自:[http://www.w3cplus.com/css3/transform-origin.html](http://www.w3cplus.com/css3/transform-origin.html)


上一篇 下一篇

猜你喜欢

热点阅读