css学习

CSS Transform

2017-03-11  本文已影响84人  Cruyun

CSS Transform(上:2D Transform)

前言及Transform 基本介绍

MDN官方文档介绍:CSS中transform 属性允许你修改CSS可视化模型的坐标空间。通过transform,可以让元素进行移动(translate)、旋转(rotate)、缩放(scale)、拉伸(skew)。本文主要介绍2D transform的基本用法及其属性的原理。

浏览器兼容性

属性

skew

rotate
绕着Transform-origin点顺时针旋转,中心点初始值为50% 50%

demo在此

目前Chrome 38+/ Opera 25+ ,我们还可以使用calc() 属性,例如transform: rotate(calc(0.25turn - 30deg))

skew和rotate的异同:

translate

scale

ps: transform: scale(-1)可以实现180°镜像对称效果

matrix

变换后的中心点坐标(x’, y’)


举个例子:transform: matrix(1, 0, 0, 1, 20, 20) 变换后的中心点横纵坐标:x' = 1 * 0 + 0 * 0 + 20 = 20, y' = 0 * 0 + 0 * 1 + 20 = 20,中心点坐标(0, 0) → (20, 20)
demo
因为中心点坐标初始值为(0, 0),则变换后的中心点坐标为(tx, ty),相当于用向量[tx,ty]进行坐标变换。transform:matrix(a, c, b, d, tx, ty)的变换效果等同于transform: translate(tx, ty), abcd的值与变换无关。
注意translate的参数需要单位,而matrix的参数的单位可省略。

Multiple values 同时使用多个属性

如```
.element{
width: 100px;
height: 200px;
transform: scale(2) skew(-20deg);
}

[demo](http://codepen.io/cruyun/pen/zZwWgP)
[CSS Tricks](https://css-tricks.com/almanac/properties/t/transform/) 提到:It’s worth noting that there is an order in which these transforms will be carried out, in the example above `skew` will be performed first and then the element will be scaled.

##Matrix与skew、scale、rotate、translate
**skew与matrix**、
`transform: matrix(1 tan(θy) tan(θx) 1 0 0)`的变换效果等同于`transform: skew( α + θx,  β + θy)`
设元素的原坐标为(x, y),进行matrix变换:![](http:https://img.haomeiwen.com/i4938344/242de94fa8b90ce1.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

变换后的横坐标x' = x+y*tan(θx) ,纵坐标y' = x*tan(θy)+y

**scale与matrix**
`transform: matrix(sx 0 0 sy 0 0)`的变换效果等同于 `transform: scale(sx, sy)`
设元素的原坐标为(x, y),进行matrix变换:![](http:https://img.haomeiwen.com/i4938344/5d205362d2069c34.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
变换后的元素坐标为(sx * x; sy * y),即x和y变为原来的sx、sy倍。

**rotate与matrix**
`transform: matrix(cosα sinα -sinα cosα 0 0)`的变换效果等同于`transform: rotate(α)`
设元素的原坐标为(x, y),进行matrix变换:![](http:https://img.haomeiwen.com/i4938344/30b97e0090aae128.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
变换后的横纵坐标分别为x' = x * cosα - y * sinα, y' = x * sinα + y * cosα

**translate与matrix**
`transform: matrix(1 0 0 1 tx ty)`的变换效果等同于`transform: translate(tx, ty)`,上文已证。
上一篇 下一篇

猜你喜欢

热点阅读