div { /*原本的狀態*/
width: 100px;
height: 100px;
background: red;
transition-property: width;/*div元素中要轉場的width屬性*/
transition-duration: 2s;/*轉場時間為2秒*/
transition-timing-function: linear;/*轉場速度為均速*/
transition-delay: 1s; /*延後1秒才開始轉場*/
}
div:hover { /*搭配偽類hover使用*/
width: 300px; /*被指定的width屬性的新數值*/
}
transition-property、transition-duration、transition-timing-function、transition-delay
transition: box-shadow 1s linear 1s
div {
width: 100px;
height: 100px;
background: red;
transition-property: width;
transition-duration: 2s;
transition-timing-function: linear;
transition-delay: 1s;
}
div:hover {
width: 300px;
}
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s linear 1s;
}
div:hover {
width: 300px;
}
上面的例子翻譯成中文:當滑鼠移動到長寬都是 100 的紅色方塊上,方塊會在一秒後,以等速且花兩秒的時間,讓寬度變成 300。
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 4s; /*為div元素中的width屬性指定持續時間為2秒的過渡效果、height屬性指定持續時間為4秒的過渡效果*/
}
div:hover {
width: 300px;
height: 500px;
}
div {
width: 100px;
height: 100px;
background: red;
transition: width 2s, height 4s;
}
div:hover {
width: 300px;
height: 500px;
}
div {
width: 100px;
height: 100px;
background: red;
}
div:hover {
transition: width 2s, height 4s;
width: 300px;
height: 500px;
}
div {
width: 100px;
height: 100px;
background: red;
transition: width 5s, height 5s; /*返回的transition屬性*/
}
div:hover {
transition: width 2s, height 2s; /*開始的transition屬性*/
width: 300px;
height: 500px;
}
div {
width: 100px;
height: 100px;
background: red;
}
div:hover {
transform:translate(50px,50px) rotate(90deg);
}
div {
width: 100px;
height: 100px;
background: red;
}
div:hover {
transform:translate(50px,50px) rotate(90deg);
}
上面的例子翻譯成中文:當滑鼠移動到長寬都是 100 的紅色方塊上,方塊會立刻向右移動 50,向下移動 50,且向右旋轉 90。
div {
width: 100px;
height: 100px;
background: red;
transform:translate(50px,50px) rotate(90deg);
}
上面的例子翻譯成中文:在向右 50,以及向下 50 的位置上,有一個長寬都是 100 ,且向右旋轉 90 的紅色方塊。
div {
width: 100px;
height: 100px;
background: red;
transition: transform 1s;
}
div:hover {
transform:translate(50px,50px) rotate(90deg);
}
上面的例子翻譯成中文:當滑鼠移動到長寬都是 100 的紅色方塊上,方塊會花一秒的時間向右移動 50,向下移動 50,且向右旋轉90。
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 2;
animation-direction: alternate;
}
@keyframes example {
0% {background-color:red; left:0px; top:0px;}
25% {background-color:yellow; left:200px; top:0px;}
50% {background-color:blue; left:200px; top:200px;}
75% {background-color:green; left:0px; top:200px;}
100% {background-color:red; left:0px; top:0px;}
}
@keyframes animation-name{ /*animation-name 動畫名稱*/
keyframes-selector{ /*關鍵影格選擇器,可以是from-to,也可以是百分比*/
css-styles; /*CSS樣式*/
}
}
div {
width: 100px;
height: 100px;
background: red;
animation:move 2s infinite;
}
@keyframes move{
from{
left:0;
}
to{
left:500px;
}
}
上面的例子翻譯成中文:長寬都是 100 的紅色方塊,花兩秒的時間從左邊向右移動500,且反覆發生。
div {
width: 100px;
height: 100px;
background: red;
animation:move 2s infinite;
}
@keyframes move{
0%{
left:0;
}
50%{
left:200px;
}
to{
left:500px;
}
}
transition-name、transition-duration、transition-timing-function、transition-delay、animation-iteration-count、animation-direction、animation-fill-mode、animation-play-state
div {
width: 100px;
height: 100px;
background-color: red;
animation: color 2s infinite, size 5s;
}
@keyframes color {
from {
background-color: red;
}
to {
background-color: yellow;
}
}
@keyframes size {
from {
width: 100px;
height: 100px;
}
to {
width: 500px;
height: 500px;
}
}