トランジションプロパティと構文を探求する
このステップでは、CSSトランジションプロパティにさらに深く入り、より複雑で正確なトランジションを作成するための完全な構文について学びます。
さまざまなトランジションプロパティを探求するためにstyles.css
ファイルを更新します。
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
gap: 20px;
}
.box {
width: 100px;
height: 100px;
background-color: #3498db;
display: flex;
justify-content: center;
align-items: center;
color: white;
cursor: pointer;
/* 完全なトランジション構文 */
transition-property: background-color, transform, border-radius;
transition-duration: 0.5s, 0.3s, 0.4s;
transition-timing-function: ease-in-out, linear, ease;
transition-delay: 0s, 0.1s, 0s;
}
.box:hover {
background-color: #2980b9;
transform: scale(1.2) rotate(15deg);
border-radius: 50%;
}
トランジションプロパティを解説しましょう。
transition-property
: トランジションするCSSプロパティを指定します。
- カンマで区切られた複数のプロパティが可能です。
all
を使用すると、すべての変更可能なプロパティにトランジションを適用できます。
transition-duration
: トランジションにかかる時間を設定します。
- 異なるプロパティには異なる期間を指定できます。
- 秒(s)またはミリ秒(ms)で指定します。
transition-timing-function
: トランジションの速度曲線を制御します。
linear
: 一定の速度
ease-in
: ゆっくりと始まり、加速する
ease-out
: 速く始まり、減速する
ease-in-out
: 始まりと終わりがゆっくりとする
transition-delay
: トランジションが始まる前に遅延を追加します。
- 連続的または交互に表示するエフェクトを作成するのに便利です。
代わりに、ショートハンドのtransition
プロパティを使用することもできます。
.box {
transition:
background-color 0.5s ease-in-out,
transform 0.3s linear 0.1s,
border-radius 0.4s ease;
}
複数のボックスを表示するためにindex.html
を更新します。
<body>
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</div>
</body>
ボックスにホバーすると、以下が見られます。
- 異なるトランジションプロパティ
- さまざまなタイミングと遅延
- 同時に複数の変換が行われる