0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【CSS】transitionプロパティについて【まとめ】

Last updated at Posted at 2020-12-13

transitionプロパティについて学習したことをまとめました。

transitionとは

ある要素の状態が変化する時、その変化が終わるまでの時間や緩急を指定できる。

とりあえず四角を用意する

index.html
  <div class="box"></div>
style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;
}

スクリーンショット 2020-12-13 17.26.30.png


:hoverした時に色を青色に変化させる

style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;
}

.box:hover {
  background: blue;
}

demo


現状では変化に間が無くて目がチカチカしてしまう

ここでtransitionの出番です

style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;

  transition: background-color 3s;

}

.box:hover {
  background-color: blue;
}

demo
第2引数に指定した3秒かけて徐々に変化している


伸ばしたり丸めたりもできます
transition: width 3s;
transition: border-radius 3s;
と毎回指定するのが面倒なときはtransition: all 3s;でOK

style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;
  transition: all 3s;
}

.box:hover {
  background-color: blue;
  width: 500px;
  border-radius: 50%;
}

demo

緩急をつける

transitionの第3引数にeaseを取ることで緩急をつけられる

デベロッパーツールで色々イジれるのが楽しそうでした。

style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;

  transition: all 3s ease;

}

.box:hover {
  background-color: blue;
  width: 500px;
  border-radius: 50%;
}

demo

demo

遅延時間の設定

第4引数に変化までの遅延時間を設定できる

style.css
.box {
  height: 100px;
  width: 100px;
  background-color: red;

  transition: all 3s ease 2s;

}

.box:hover {
  background-color: blue;
  width: 500px;
  border-radius: 50%;
}

demo

2秒待ったあとに変化が始まる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?