はじめに
本記事ではHTML、CSSを使用しテキストに横スライドアニメーションをつける方法について記載します。
ゴール
次のような横スライドアニメーションを作成する事を本記事のゴールとする。
ソース
HTML
htmlファイルを準備します。今回は横からテキストをスライド表示
というht
タグのテキストをスライド表示させます。
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>横スライドアニメーション</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1 class="animated-text">横からテキストをスライド表示</h1>
</div>
</body>
</html>
CSS
CSSファイルを準備します。
keyframes
で横スライドアニメーション作成します。transform
プロパティで画面外から横方向にスライドさせます。
styles.css
body {
padding: 0;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100vh;
background-color: #f0f0f0;
}
.container {
text-align: center;
}
.animated-text {
opacity: 0;
animation: slideIn 1s forwards;
}
@keyframes slideIn {
from {
transform: translateX(-100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
実行 & 結果確認
サーバを起動し動作を確認すると下記画像のように、横スライドアニメーションが適用されている事を確認できます。