Blog

初心者でもできる!純粋なJavaScriptで自作スライドショーを作る方法【完全ガイド】

2025年04月13日

「JavaScriptだけで動くスライドショーを作ってみたいけど難しそう…」そんな方へ。

この記事では、jQueryや外部ライブラリを使わずに「素のJavaScript(Vanilla JS)」だけで実装するスライドショーの作り方を、初心者にもわかりやすくステップごとに解説します。

「とにかく軽くて速いサイトを作りたい」「自分でコードを理解しながら実装したい」そんなWeb制作者・WordPressユーザーにおすすめの内容です。

<article class="top-01a">
<section id="tslide" class="tslide">
    <div>
      <p>スライド1</p>
    </div>
    <div>
      <p>スライド2</p>
    </div>
    <div>
      <p>スライド3</p>
    </div>
  </section>
<!-- 左右のボタン -->
<span id="prev" class="prev"></span>
  <span id="next" class="next"></span>
  <!-- インジケーター -->
  <ul class="indicator" id="indicator">
    <li class="list"></li>
    <li class="list"></li>
    <li class="list"></li>
  </ul>
</article>
.tslide {
  /*スライド全体 */
  width: 300%;
  height: 100%;
  display: flex;
  transition: all 0.3s;
}

.tslide div {
  /* スライドの中身 */
  width: 33.33%;
  height: 100%;
  font-size: 16px;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
}

.tslide1 {
  /* スライドさせるために必要なクラス */
  transform: translateX(0);
}

.tslide2 {
  /* スライドさせるために必要なクラス */
  transform: translateX(-33.33%);
}

.tslide3 {
  /* スライドさせるために必要なクラス */
  transform: translateX(-66.66%);
}

.tslide div:nth-of-type(1) {
  /* 背景色 */
  background-image: url(img/top1.png);
  background-size: cover;
  background-position: right;
}

.tslide div:nth-of-type(2) {
  /* 背景色 */
  background-image: url(img/top2.png);
  background-size: cover;
}

.tslide div:nth-of-type(3) {
  /* 背景色 */
  background-image: url(img/top3.png);
  background-size: cover;
}

/* ↓ 左右のボタン */
.next {
  position: absolute;
  width: 15px;
  height: 15px;
  right: 10px;
  bottom: 50%;
  z-index: 10;
  cursor: pointer;
  border-top: solid 3px #000;
  border-right: solid 3px #000;
  -webkit-transform: rotate(45deg) translateY(50%);
  transform: rotate(45deg) translateY(50%);
}

.prev {
  position: absolute;
  width: 15px;
  height: 15px;
  left: 25px;
  bottom: 50%;
  z-index: 10;
  cursor: pointer;
  border-top: solid 3px #000;
  border-right: solid 3px #000;
  -webkit-transform: rotate(-135deg) translateY(-50%);
  transform: rotate(-135deg) translateY(-50%);
}

/* ↓ インジケーター */
.indicator {
  width: 100%;
  position: absolute;
  bottom: 20px;
  display: flex;
  column-gap: 18px;
  z-index: 10;
  justify-content: center;
  align-items: center;
}

.indicator li {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  list-style: none;
  background-color: #fff;
  border: 2px #000 solid;
  cursor: pointer;
}

.indicator li:first-of-type {
  background-color: #000;
}
const slide = document.getElementById('tslide');
const prev = document.getElementById('prev');
const next = document.getElementById('next');
const indicator = document.getElementById('indicator');
const lists = document.querySelectorAll('.list');
const totalSlides = lists.length;
let count = 0;
let autoPlayInterval;
function updateListBackground() {
	for (let i = 0; i < lists.length; i++) {
		lists[i].style.backgroundColor = i === count % totalSlides ? '#000' : '#fff';
	}
}
function nextClick() {
	slide.classList.remove(`tslide${count % totalSlides + 1}`);
	count++;
	slide.classList.add(`tslide${count % totalSlides + 1}`);
	updateListBackground();
}
function prevClick() {
	slide.classList.remove(`tslide${count % totalSlides + 1}`);
	count--;
	if (count < 0) count = totalSlides - 1;
	slide.classList.add(`tslide${count % totalSlides + 1}`);
	updateListBackground();
}
function startAutoPlay() {
	autoPlayInterval = setInterval(nextClick, 5000);
}
function resetAutoPlayInterval() {
	clearInterval(autoPlayInterval);
	startAutoPlay();
}
next.addEventListener('click', () => {
	nextClick();
	resetAutoPlayInterval();
});
prev.addEventListener('click', () => {
	prevClick();
	resetAutoPlayInterval();
});
indicator.addEventListener('click', (event) => {
	if (event.target.classList.contains('list')) {
		const index = Array.from(lists).indexOf(event.target);
		slide.classList.remove(`tslide${count % totalSlides + 1}`);
		count = index;
		slide.classList.add(`tslide${count % totalSlides + 1}`);
		updateListBackground();
		resetAutoPlayInterval();
	}
});
startAutoPlay();

参考にしたサイトはこちら▼

ぜひみんなも試してみてね!

  • Categories