全画面カルーセルを作る方法【HTML/CSS/JavaScript・サンプルコード付き】
カルーセル
全画面カルーセルを作る方法
全画面カルーセルをHTML・CSS・JavaScriptで作る方法を紹介します。ブラウザ全体を使って大きな画像やメッセージを表示できるため、トップページのメインビジュアルやキャンペーン告知、サービス紹介などに適しています。画面いっぱいに表示されるため視認性が高く、訪問者へ伝えたい情報を印象的に見せられます。
このサンプルでは、画面いっぱいに表示されるカルーセルに加え、前へ・次へボタン、ドットナビ、自動再生を実装しています。JavaScriptでスライドの切り替えを行い、CSSで全画面レイアウトやアニメーションを設定しているため、さまざまなWebサイトへそのまま組み込みやすい構成です。
スライドの切り替え速度や自動再生の間隔はコード内の数値を変更するだけで簡単に調整できます。企業サイト、サービス紹介ページ、ポートフォリオ、ランディングページなど、ファーストビューを大きく見せたい場面で活用できます。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
デモ
このデモでは、ブラウザいっぱいに表示される全画面カルーセルを確認できます。スライドは3秒ごとに自動で切り替わり、前へ・次へボタンやドットナビからも自由に操作できます。
壮大な景色を全画面表示
ファーストビュー全体を使って印象的なビジュアルを表示できます。
詳しく見る
サービスを魅力的に紹介
大きな画像とメッセージで訪問者へ強くアピールできます。
詳細を見る
キャンペーンを告知
ランディングページや企業サイトにもおすすめです。
確認する
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コード -->
<meta charset="UTF-8">
<!-- スマホ表示に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>全画面カルーセル</title>
<!-- CSSファイルを読み込み -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- 全画面カルーセル全体 -->
<div class="fullscreen_carousel_demo">
<!-- スライド一覧 -->
<div class="fullscreen_carousel_track" id="fullscreen-carousel-track">
<!-- スライド1 -->
<div class="fullscreen_carousel_slide">
<div class="fullscreen_carousel_content">
<div class="fullscreen_carousel_title">壮大な景色を全画面表示</div>
<p class="fullscreen_carousel_text">
ファーストビュー全体を使って印象的なビジュアルを表示できます。
</p>
<a href="#" class="fullscreen_carousel_link">詳しく見る</a>
</div>
</div>
<!-- スライド2 -->
<div class="fullscreen_carousel_slide">
<div class="fullscreen_carousel_content">
<div class="fullscreen_carousel_title">サービスを魅力的に紹介</div>
<p class="fullscreen_carousel_text">
大きな画像とメッセージで訪問者へ強くアピールできます。
</p>
<a href="#" class="fullscreen_carousel_link">詳細を見る</a>
</div>
</div>
<!-- スライド3 -->
<div class="fullscreen_carousel_slide">
<div class="fullscreen_carousel_content">
<div class="fullscreen_carousel_title">キャンペーンを告知</div>
<p class="fullscreen_carousel_text">
ランディングページや企業サイトにもおすすめです。
</p>
<a href="#" class="fullscreen_carousel_link">確認する</a>
</div>
</div>
</div>
<!-- 前へボタン -->
<button
class="fullscreen_carousel_arrow fullscreen_carousel_prev"
id="fullscreen-carousel-prev"
type="button"
aria-label="前へ">
前へ
</button>
<!-- 次へボタン -->
<button
class="fullscreen_carousel_arrow fullscreen_carousel_next"
id="fullscreen-carousel-next"
type="button"
aria-label="次へ">
次へ
</button>
<!-- ドットナビ -->
<div class="fullscreen_carousel_dots">
<button
class="fullscreen_carousel_dot is-active"
type="button"
data-index="0"
aria-label="スライド1">
</button>
<button
class="fullscreen_carousel_dot"
type="button"
data-index="1"
aria-label="スライド2">
</button>
<button
class="fullscreen_carousel_dot"
type="button"
data-index="2"
aria-label="スライド3">
</button>
</div>
</div>
<!-- JavaScriptファイルを読み込み -->
<script src="script.js"></script>
</body>
</html>
/* ページ全体 */
body{
margin:0; /* ページ外側の余白 */
font-family:sans-serif; /* 使用するフォント */
background:#0f172a; /* ページ背景色 */
}
/* 全画面カルーセル全体 */
.fullscreen_carousel_demo{
position:relative; /* ボタンとドット配置の基準 */
width:100%; /* 横幅いっぱい */
height:100vh; /* 画面高さいっぱい */
min-height:520px; /* 最小高さ */
overflow:hidden; /* はみ出したスライドを隠す */
font-family:sans-serif; /* 使用するフォント */
}
/* スライド一覧 */
.fullscreen_carousel_track{
display:flex; /* 横並び */
width:100%; /* 横幅いっぱい */
height:100%; /* 高さいっぱい */
transition:transform .5s ease; /* スライド速度:.5sを大きくすると遅く、小さくすると速くなる */
}
/* スライド共通 */
.fullscreen_carousel_slide{
flex:0 0 100%; /* 1画面に1枚表示 */
position:relative; /* 文字配置の基準 */
display:flex; /* 中央配置 */
align-items:center; /* 縦中央 */
justify-content:center; /* 横中央 */
color:#ffffff; /* 文字色 */
background-size:cover; /* 背景画像を全面表示 */
background-position:center; /* 背景画像を中央表示 */
}
/* スライド1 */
.fullscreen_carousel_slide:nth-child(1){
background:linear-gradient(rgba(15,23,42,.45),rgba(15,23,42,.45)),url("images/【配置した画像のファイル名1】"); /* 画像1:暗いベール付き */
}
/* スライド2 */
.fullscreen_carousel_slide:nth-child(2){
background:linear-gradient(rgba(15,23,42,.45),rgba(15,23,42,.45)),url("images/【配置した画像のファイル名2】"); /* 画像2:暗いベール付き */
}
/* スライド3 */
.fullscreen_carousel_slide:nth-child(3){
background:linear-gradient(rgba(15,23,42,.45),rgba(15,23,42,.45)),url("images/【配置した画像のファイル名3】"); /* 画像3:暗いベール付き */
}
/* 文字エリア */
.fullscreen_carousel_content{
max-width:700px; /* 文章の最大横幅 */
padding:20px; /* 内側余白 */
text-align:center; /* 中央揃え */
}
/* タイトル */
.fullscreen_carousel_title{
margin:0 0 20px; /* 下余白 */
font-size:52px; /* タイトル文字サイズ */
font-weight:700; /* 文字の太さ */
}
/* 説明文 */
.fullscreen_carousel_text{
margin:0 0 30px; /* 下余白 */
font-size:18px; /* 文字サイズ */
line-height:1.8; /* 行間 */
}
/* リンクボタン */
.fullscreen_carousel_link{
display:inline-flex; /* ボタン型 */
align-items:center; /* 縦中央 */
justify-content:center; /* 横中央 */
min-width:180px; /* 最小横幅 */
height:50px; /* ボタン高さ */
border-radius:999px; /* 丸いボタン */
background:#ffffff; /* 背景色 */
color:#2563eb; /* 文字色 */
text-decoration:none; /* 下線なし */
font-weight:700; /* 文字の太さ */
}
/* 矢印ボタン */
.fullscreen_carousel_arrow{
position:absolute; /* 画面上に固定配置 */
top:50%; /* 縦中央 */
width:48px; /* ボタン横幅 */
height:48px; /* ボタン高さ */
padding:0; /* 内側余白なし */
border:none; /* 枠線なし */
border-radius:50%; /* 円形 */
background:#ffffff; /* 背景色 */
cursor:pointer; /* カーソル */
transform:translateY(-50%); /* 縦中央補正 */
font-size:0; /* ボタン内テキストを非表示 */
z-index:5; /* スライドより前面 */
}
/* 矢印アイコン */
.fullscreen_carousel_arrow::before{
content:""; /* 矢印用 */
position:absolute; /* ボタン内に配置 */
top:50%; /* 縦中央 */
left:50%; /* 横中央 */
width:12px; /* 矢印横幅 */
height:12px; /* 矢印高さ */
border-top:3px solid #2563eb; /* 矢印線 */
border-right:3px solid #2563eb; /* 矢印線 */
}
/* 前へボタン */
.fullscreen_carousel_prev{
left:24px; /* 左からの位置 */
}
/* 前へ矢印 */
.fullscreen_carousel_prev::before{
transform:translate(-40%,-50%) rotate(-135deg); /* 左向き矢印 */
}
/* 次へボタン */
.fullscreen_carousel_next{
right:24px; /* 右からの位置 */
}
/* 次へ矢印 */
.fullscreen_carousel_next::before{
transform:translate(-60%,-50%) rotate(45deg); /* 右向き矢印 */
}
/* ドットナビ */
.fullscreen_carousel_dots{
position:absolute; /* 画面下部に配置 */
left:0; /* 左端 */
right:0; /* 右端 */
bottom:28px; /* 下からの位置 */
display:flex; /* 横並び */
justify-content:center; /* 中央寄せ */
gap:12px; /* ドット間隔 */
z-index:5; /* スライドより前面 */
}
/* ドット */
.fullscreen_carousel_dot{
width:12px; /* ドット横幅 */
height:12px; /* ドット高さ */
padding:0; /* 内側余白なし */
border:none; /* 枠線なし */
border-radius:50%; /* 円形 */
background:rgba(255,255,255,.5); /* 通常時の色 */
cursor:pointer; /* カーソル */
}
/* 選択中ドット */
.fullscreen_carousel_dot.is-active{
background:#ffffff; /* 選択中の色 */
}
/* タブレット・スマホ表示 */
@media(max-width:768px){
.fullscreen_carousel_title{
font-size:34px; /* 小画面時のタイトル文字サイズ */
}
.fullscreen_carousel_text{
font-size:16px; /* 小画面時の説明文サイズ */
}
}
// スライド一覧を横方向に動かす要素を取得
const fullscreenCarouselTrack = document.getElementById("fullscreen-carousel-track");
// スライドをすべて取得
const fullscreenCarouselSlides = document.querySelectorAll(".fullscreen_carousel_slide");
// ドットナビをすべて取得
const fullscreenCarouselDots = document.querySelectorAll(".fullscreen_carousel_dot");
// 前へボタンを取得
const fullscreenCarouselPrev = document.getElementById("fullscreen-carousel-prev");
// 次へボタンを取得
const fullscreenCarouselNext = document.getElementById("fullscreen-carousel-next");
// 現在表示しているスライド番号
let fullscreenCarouselCurrent = 0;
// 自動再生間隔
const fullscreenCarouselInterval = 3000; // 3000 = 3秒ごとに次のスライドへ切り替える
// スライドを表示する
function fullscreenCarouselShow(index){
// 1枚目より前へ移動した場合は最後へ移動
if(index < 0){
fullscreenCarouselCurrent = fullscreenCarouselSlides.length - 1;
// 最後より次へ移動した場合は1枚目へ戻る
}else if(index >= fullscreenCarouselSlides.length){
fullscreenCarouselCurrent = 0;
// 通常の移動
}else{
fullscreenCarouselCurrent = index;
}
// 現在の番号に合わせて横方向へスライド
fullscreenCarouselTrack.style.transform =
"translateX(-" + (fullscreenCarouselCurrent * 100) + "%)";
// すべてのドットから選択状態を外す
fullscreenCarouselDots.forEach(function(dot){
dot.classList.remove("is-active");
});
// 現在のスライドに対応するドットを選択状態にする
fullscreenCarouselDots[fullscreenCarouselCurrent].classList.add("is-active");
}
// 前へボタンをクリックした時
fullscreenCarouselPrev.addEventListener("click", function(){
// 前のスライドへ移動
fullscreenCarouselShow(fullscreenCarouselCurrent - 1);
});
// 次へボタンをクリックした時
fullscreenCarouselNext.addEventListener("click", function(){
// 次のスライドへ移動
fullscreenCarouselShow(fullscreenCarouselCurrent + 1);
});
// ドットナビをクリックした時
fullscreenCarouselDots.forEach(function(dot){
dot.addEventListener("click", function(){
// クリックしたドットの番号へ移動
fullscreenCarouselShow(Number(this.dataset.index));
});
});
// 自動再生
setInterval(function(){
// 一定時間ごとに次のスライドへ移動
fullscreenCarouselShow(fullscreenCarouselCurrent + 1);
}, fullscreenCarouselInterval);
// 初期表示
fullscreenCarouselShow(0);
ファイル構成と設置方法
このサンプルは、HTML・CSS・JavaScriptの3ファイルで構成されています。HTMLで全画面カルーセルのレイアウトを作成し、CSSで全画面表示や背景画像、ボタン、ドットナビのデザインを設定します。JavaScriptでは前へ・次へボタン、ドットナビ、自動再生によるスライド切り替えを制御しています。
sample
├── index.html
├── style.css
├── script.js
└── images
├── 【配置した画像のファイル名1】
├── 【配置した画像のファイル名2】
└── 【配置した画像のファイル名3】
画像を sample/images/ に配置し、sample/index.html をブラウザで開くと動作を確認できます。背景画像は style.css の url("images/ファイル名") を変更して設定してください。タイトルや説明文、ボタンの文字は index.html で編集できます。スライドの切り替え速度は style.css の transition: transform .5s ease; を変更します。.5s = 0.5秒で、数値を大きくすると切り替えがゆっくりになり、小さくすると速くなります。自動再生の間隔は script.js の const fullscreenCarouselInterval = 3000; を変更します。3000 = 3秒で、数値を大きくするとスライドの表示時間が長くなり、小さくすると短い間隔で切り替わります。
全画面カルーセルを作る方法【HTML/CSS/JavaScript・サンプルコード付き】
コメント