LoginSignup
0

More than 1 year has passed since last update.

posted at

jQueryライブラリを使わないスライドショー

これまでスライドショーといえば業務で何度もslick等のライブラリにお世話になってきましたが、ネイティブJavaScriptとCSSで実装にチャレンジです。
今回は海外のフロントエンドエンジニアさんがYouTubeで紹介されていた「自動スライド用のラジオボタンと、手動スライド用のラジオボタンの2種類を重ねて設置する」方法を参考にしています。

HTML

   <div class="slider">
      <div class="slide_container">
        <!-- ラジオボタン start -->
        <input type="radio" name="radioBtn" id="radio_btn1" />
        <input type="radio" name="radioBtn" id="radio_btn2" />
        <input type="radio" name="radioBtn" id="radio_btn3" />
        <input type="radio" name="radioBtn" id="radio_btn4" />
        <!-- ラジオボタン end -->
        <!-- スライド画像 start -->
        <div class="slide_item start">
          <img src="image_1.jpg" alt="スライド画像1" />
        </div>
        <div class="slide_item">
          <img src="image_2.jpg" alt="スライド画像2" />
        </div>
        <div class="slide_item">
          <img src="image_3.jpg" alt="スライド画像3" />
        </div>
        <div class="slide_item">
          <img src="image_4.jpg" alt="スライド画像4" />
        </div>
        <!-- スライド画像 end -->
        <!-- 自動スライド start -->
        <div class="slide_auto">
          <div class="auto_btn1"></div>
          <div class="auto_btn2"></div>
          <div class="auto_btn3"></div>
          <div class="auto_btn4"></div>
        </div>
        <!-- 自動スライド end -->
      </div>
      <!-- 手動スライド start -->
      <div class="slide_manual">
        <label for="radio_btn1" class="manual_btn"></label>
        <label for="radio_btn2" class="manual_btn"></label>
        <label for="radio_btn3" class="manual_btn"></label>
        <label for="radio_btn4" class="manual_btn"></label>
      </div>
      <!-- 手動スライド end -->
    </div>

CSS

※スライド画像サイズ600 × 400px想定の値。ラジオボタンは画像の外に出しています。

body {
  margin: 0;
  padding: 0;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #555555;
}
.slider {
  width: 600px;
  height: 400px;
  overflow: hidden;
}
.slide_container {
  width: 400%;
  height: 400px;
  display: flex;
}
.slide_container input {
  display: none;
}
.slide_item {
  transition: 1s;
}
.slide_item img {
  width: 600px;
  height: 400px;
}
/* 手動スライド */
.slide_manual {
  position: absolute;
  width: 600px;
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
.manual_btn {
  border: 2px solid #78aeff;
  padding: 5px;
  border-radius: 10px;
  cursor: pointer;
}
.manual_btn:not(:last-child) {
  margin-right: 40px;
}
.manual_btn:hover {
  background-color: #78aeff;
}
#radio_btn1:checked ~ .start {
  margin-left: 0;
}
#radio_btn2:checked ~ .start {
  margin-left: -600px;
}
#radio_btn3:checked ~ .start {
  margin-left: -1200px;
}
#radio_btn4:checked ~ .start {
  margin-left: -1800px;
}
/* 自動スライド */
.slide_auto {
  position: absolute;
  display: flex;
  width: 600px;
  justify-content: center;
  margin-top: 410px;
}
.slide_auto div {
  border: 2px solid #78aeff;
  padding: 5px;
  border-radius: 10px;
  transition: 1s;
}
.slide_auto div:not(:last-child) {
  margin-right: 40px;
}
#radio_btn1:checked ~ .slide_auto .auto_btn1 {
  background-color: #78aeff;
}
#radio_btn2:checked ~ .slide_auto .auto_btn2 {
  background-color: #78aeff;
}
#radio_btn3:checked ~ .slide_auto .auto_btn3 {
  background-color: #78aeff;
}
#radio_btn4:checked ~ .slide_auto .auto_btn4 {
  background-color: #78aeff;
}

JavaScript

ラジオボタンを1~順番に設定した間隔でチェックを入れていく。
オートスライドの速さは5000(ミリ秒)の値を変更して調整。

      let counter = 1;
      setInterval(function(){
        document.getElementById('radio_btn' + counter).checked = true;
        counter++;
        if(counter > 4){
          counter = 1;
        }
      }, 5000);

実現はできたのですが、現状では「手動でスライドを操作してる間も自動スライドは動き続けている」という懸念点があり、サイト上に実装するにはまだ改善が必要そうです。

ただ、今回ライブラリに頼らず、一つ一つ記述しては表示や挙動を確認していくと、「今何をしたくてこれを書いているのか」が明確になり、期待と違う挙動を見せた時も「ここまでは合っている→これ以降に原因がありそう」という具合に早く問題個所を絞り込めますね。
ライブラリはとても便利ですが、それに甘えすぎないよう精進し続けたいものです。

参考にさせていただいた動画:https://www.youtube.com/watch?v=0wvrlOyGlq0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0