一、2D变换
1、transform 设置或检索对象的转换
取值:
none::以一个含六值的(a,b,c,d,e,f)变换矩阵的形式指定一个2D变换,相当于直接应用一个[a,b,c,d,e,f]变换矩阵
translate(
translateX(
translateY(
rotate(
scaleX(
scaleY(
skew(
skewY(
注:不同浏览器需写以下前缀。
内核类型 | 写法 |
---|---|
Webkit(Chrome/Safari) | -webkit-transform |
Gecko(Firefox) | -moz-transform |
Presto(Opera) | -o-transform |
Trident(IE) | -ms-transform |
W3C | transform |
例子:
CSS代码:
<span style="font-size: 14px;"><span style="color: #800000;"> #transform1 </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> mediumvioletred</span>;<span style="color: #ff0000;"> -webkit-transform</span>:<span style="color: #0000ff;"> rotate(15deg)</span>; }</span>
HTML代码:
<span style="font-size: 15px;"><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="transform1"</span><span style="color: #0000ff;">></span>旋转了15度<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span>
2、transform-origin 设置或检索对象以某个原点进行转换。
取值:
left:指定原点的横坐标为leftcenter①:指定原点的横坐标为
centerright:指定原点的横坐标为
righttop:指定原点的纵坐标为
topcenter②:指定原点的纵坐标为
centerbottom:指定原点的纵坐标为bottom
不同浏览器下的写法:
内核类型 | 写法 |
---|---|
Webkit(Chrome/Safari) | -webkit-transform-origin |
Gecko(Firefox) | -moz-transform-origin |
Presto(Opera) | -o-transform-origin |
Trident(IE) | -ms-transform-origin |
W3C | transform-origin |
例子:
CSS代码:
<span style="font-size: 15px;"><span style="color: #800000;"> #transform1 </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> mediumvioletred</span>;<span style="color: #ff0000;"> -webkit-transform</span>:<span style="color: #0000ff;"> rotate(15deg)</span>;/*旋转15度*/<span style="color: #ff0000;"> -webkit-transform-origin</span>:<span style="color: #0000ff;"> left top</span>; /*以左上角为原点旋转*/ } </span>
HTML代码:
<span style="font-size: 15px;"><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="transform1"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span>
二、过渡样式
1、transition-property 检索或设置对象中的参与过渡的属性。
取值:
<span style="font-size: 15px;"><span style="color: #800000;"> #transform1 </span>{<span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0 auto</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> red</span>;<span style="color: #ff0000;"> transition-property</span>:<span style="color: #0000ff;"> background-color</span>; }<span style="color: #800000;"> #transform1:hover </span>{<span style="color: #ff0000;"> transition-duration</span>:<span style="color: #0000ff;">.5s</span>;<span style="color: #ff0000;"> transition-timing-function</span>:<span style="color: #0000ff;">ease-in</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> yellow</span>; } </span>
<span style="font-size: 15px;"><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="transform1"</span><span style="color: #0000ff;">></span>请将鼠标放在上面<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span>
<span style="font-size: 15px;"><span style="color: #800000;"> #delay1 </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> antiquewhite</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;">100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;">100px</span>; }<span style="color: #800000;"> #delay1:hover </span>{<span style="color: #ff0000;"> transition-delay</span>:<span style="color: #0000ff;">1s</span>;<span style="color: #ff0000;"> transition-timing-function</span>:<span style="color: #0000ff;">ease-in</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> black</span>; }<span style="color: #800000;"> #delay2 </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> antiquewhite</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;">100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;">100px</span>; }<span style="color: #800000;"> #delay2:hover </span>{<span style="color: #ff0000;"> transition-delay</span>:<span style="color: #0000ff;">4s</span>;<span style="color: #ff0000;"> transition-timing-function</span>:<span style="color: #0000ff;">ease-in</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> blue</span>; } </span>
<span style="font-size: 15px;"><span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="delay1"</span><span style="color: #0000ff;">><span style="color: #000000;">延迟</span></span>1s后开始过渡<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="delay2"</span><span style="color: #0000ff;">></span>延迟4s后开始过渡<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span>
<span style="font-size: 15px;"><span style="color: #800000;"> #all </span>{<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> red</span>; }<span style="color: #800000;"> #all:hover </span>{<span style="color: #ff0000;"> background-color</span>:<span style="color: #0000ff;"> yellow</span>;<span style="color: #ff0000;"> transition-delay</span>:<span style="color: #0000ff;"> .5s</span>;<span style="color: #ff0000;"> transition-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;"> transform</span>:<span style="color: #0000ff;"> scale(1.5,1.5)</span>;<span style="color: #ff0000;"> transition-duration</span>:<span style="color: #0000ff;"> 1s</span>; } </span>
<span style="font-size: 15px;"><span style="color: #0000ff;"> <span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="all"</span><span style="color: #0000ff;">></span>请把鼠标放在上面查看效果<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span>