basic
基本は、変化前と変化後のCSSを作成して、その時間制御を行う
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>CSSの練習</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="box"></div>
</body>
</html>
body {
padding: 64px;
}
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: border-radius ;
transition-duration: 1s ;
}
.box:hover {
border-radius: 50%;
}
transform 変形
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform ;
transition-duration: 1s ;
}
.box:hover {
transform: translate(50px, 20px);
transform: rotate(30deg);
transform: scale(0.5, 2);
transform: translateX(100px) rotate(30deg);
transform: rotate(30deg) translateX(100px);
}
transform-origin 起点
変形の起点の変更
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform ;
transition-duration: 1s ;
transform-origin: top left;
}
.box:hover {
transform: translate(50px, 20px);
transform: rotate(30deg);
transform: scale(0.5, 2);
/* transform: translateX(100px) rotate(30deg);
transform: rotate(30deg) translateX(100px); */
}
transition-property
移動と色の変化に適用
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform ,background;
transition-duration: 1s ;
}
.box:hover {
transform: translateX(30px);
background: skyblue;
}
transition の時間制御
duration
は動作時間、delay
は、開始時間の制御
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform;
transition-duration: .3s ;
transition-delay: 1s ;
}
.box:hover {
transform: translateX(30px);
}
transition-timing-function
デベロッパーツールでの確認
3字ペジュエディタで確認
body {
padding: 64px;
}
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform;
transition-duration: .3s ;
/* transition-timing-function: ease-out; */
transition-timing-function: cubic-bezier(0, 0.61, 0.88, 0.59);
}
.box:hover {
transform: translateX(30px);
}
transition関連のプロパティを一括で指定する
順番は、プロパティ指定、duration, ease-out, delay
複数指定は、カンマで続ける
.box {
width: 100px;
height: 100px;
background: orange;
transition-property: transform;
transition-duration: .3s ;
/* transition-timing-function: ease-out; */
/* transition-timing-function: cubic-bezier(0, 0.61, 0.88, 0.59); */
transition: transform .3s ease-out 1s, background .5s;
}
.box:hover {
transform: translateX(30px);
background: skyblue;
}
animationプロパティ
先ほどとの違いは、hoverなどの動作変化後の指定がないこと
ロードされた時点で実行される
keyframes
body {
padding: 64px;
}
.box {
width: 100px;
height: 100px;
background: orange;
animation-name: move;
animation-duration: 1s;
}
@keyframes move {
0% {
transition: none;
}
80% {
transform: translateX(200px) rotate(360deg);
}
100% {
transform: translateX(300px) rotate(360deg);
}
}
animationの制御
.box {
width: 100px;
height: 100px;
background: orange;
animation-name: move;
animation-duration: 2s;
/* animation-delay: 1s; */
/* animation-fill-mode: forwards; */
animation-iteration-count: 2;
animation-iteration-count: infinite;
}
再生順の制御
.box {
width: 100px;
height: 100px;
background: orange;
animation-name: move;
animation-duration: 2s;
animation-iteration-count: infinite;
/* animation-direction: alternate; */
/* animation-direction: reverse; */
/* animation-direction: alternate-reverse; */
/* animation-timing-function: linear; */
}
@keyframes move {
0% {
transition: none;
animation-timing-function: linear;
}
80% {
transform: translateX(200px) rotate(360deg);
}
100% {
transform: translateX(300px) rotate(360deg);
}
}
animation関連のプロパティを一括で指定
.box {
width: 100px;
height: 100px;
background: orange;
/* animation-name: move;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-delay: 1s; */
animation: move 2s infinite 1s;
}
ふわっとかわるボタン
opacityがよく使われる表現
<body>
<span class="btn">OK</span>
</body>
body {
padding: 64px;
}
.btn {
padding: 16px 32px;
background: hsl(200, 100%, 40%);
color: #fff;
box-shadow: 0 1px 4px rgba(0,0,0,0.1);
cursor: pointer;
transition: background .3s;
}
.btn:hover {
background: hsl(200, 100%, 45%);
}
回転するアイコン ローディングアイコン
cssだけで書ける
<body>
<div class="loading"></div>
</body>
body {
padding: 64px;
}
.loading {
width: 40px;
height: 40px;
border: 8px solid #ccc;
border-right-color: transparent;
border-radius: 50%;
animation: spin .8s infinite linear;
}
@keyframes spin {
from {
transform: none;
}
to {
transform: rotate(360deg);
}
}
キーフレームを使ってポップアップのアニメーション
<body>
<div class="message">Hello!</div>
</body>
body {
padding: 64px;
}
.message {
width: 300px;
padding: 8px 16px;
background: #2c2c2c;
color: #ddd;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
position: fixed;
right: 32px;
bottom: 32px;
}
メッセージアニメーション
下からふわっと出てきて消える
.message {
width: 300px;
padding: 8px 16px;
background: #2c2c2c;
color: #ddd;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
position: fixed;
right: 32px;
bottom: 32px;
animation: popup 2s forwards;
}
@keyframes popup {
0% {
transform: translateY(20px);
opacity: 0;
animation-timing-function: ease-in-out;
}
20%, 80% {
transform: none;
opacity: 1;
}
100% {
transform: translateY(20px);
opacity: 0;
}
}
pointer-eventsの設定
画面からは消えているが、実際はみえないだけでのこっているので、ボタンなどが効かなくなる
<body>
<button>Click me!</button>
<div class="message">Hello!</div>
<script> </script>
</body>
body {
padding: 64px;
}
.message {
width: 300px;
padding: 8px 16px;
background: #2c2c2c;
color: #ddd;
border-radius: 4px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
position: fixed;
right: 32px;
bottom: 32px;
animation: popup 2s forwards;
}
@keyframes popup {
0% {
transform: translateY(20px);
opacity: 0;
animation-timing-function: ease-out;
}
20%, 80% {
transform: none;
opacity: 1;
}
100% {
transform: translateY(20px);
opacity: 0;
pointer-events: none;
}
}
button {
width: 300px;
padding: 8px 16px;
position: fixed;
right: 32px;
bottom: 32px;
}