0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

スライドショーをJavascriptで実装してみた 5日目【WEBサイトを作る30日チャレンジ】

Posted at

簡単なWEBサイトにスライドショーをJavascriptを使って実装

 ・なるべくレスポンシブ(スマホ最適化)を意識
 ・画像が一定時間で切り替わる仕様にします(今回は5枚用意)
 ・画像はフリーの画像を使用しました(photo-acさん、ありがとうございます)
  https://www.photo-ac.com/
 ・WEBサイトの基盤は前回作成したものを活用します
  https://qiita.com/pontarou194/items/fa08a2f0f0fe4cd2ab20
 ・画像にalt属性つけてますが、つけなくてもいいです・・・(SEOを意識してしまう病です)

完成したWEBサイトがこちら

ezgif.com-video-to-gif (2).gif

コードはこちら

HTML + Javascript

<!DOCTYPE html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="30_5.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://kit.fontawesome.com/a076d05399.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.0.0/animate.min.css"/>
    <title>スライドショーCSS+JS</title>
  </head>
  <body>
    <nav>
      <input type="checkbox" id="check">
      <label for="check" class="checkbtn">
        <i class="fas fa-bars"></i>
      </label>
      <label class="logo"><a href="#" class="logo animate_bounce">Cool♪</a> </label>
      <ul>
        <li><a class="active" href="#">ホーム</a></li>
        <li><a href="#">会社概要</a></li>
        <li><a href="#">事業内容</a></li>
        <li><a href="#">お問い合わせ</a></li>
        <li><a href="#">採用情報</a></li>
      </ul>
    </nav>
    <div class="content">
      <div class="images">
        <img src="3125822_s.jpg" alt="ビジネスシーン">
        <img src="3271507_s.jpg" alt="OL">
        <img src="3123552_s.jpg" alt="柴犬">
        <img src="3411723_s.jpg" alt="オンライン飲み会">
        <img src="3452134_s.jpg" alt="オンライン学習">
      </div>
    </div>
      <div class="center">
        <div class="outer button">
          <button>ログイン</button>
        </div>
        <div class="outer circle">
          <button>新規登録</button>
        </div>
      </div>
    </body>
  <script>
    let indexValue = 0;
    function slideShow(){
      setTimeout(slideShow, 2500);
      var x;
      const img = document.querySelectorAll("img");
      for(x = 0; x < img.length; x++){
        img[x].style.display = "none";
      }
      indexValue++;
      if(indexValue > img.length){indexValue = 1}
      img[indexValue -1].style.display = "block";
    }
    slideShow();
    </script>
  </body>
</html>

CSS

@import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
}

html{
  background-color: black;
}

nav{
  background: black;
  height: 80px;
  width: 100%;
}
label.logo a{
  color: white;
  font-size: 33px;
  line-height: 80px;
  font-weight: 600;
}
nav ul{
  float: right;
  margin-right: 60px;
}
nav ul li{
  display: inline-block;
  line-height: 80px;
  margin: 0 2px;
}
nav ul li a{
  color: #f2f2f2;
  font-weight: 500;
  font-size: 20px;
  padding: 7px 13px;
  border-radius: 3px;
  text-transform: uppercase;
  font-family: 'Poppins', sans-serif;
}
a.active,a:hover{
  background: black;
  transition: .5s;
}
.checkbtn{
  font-size: 30px;
  color: white;
  float: right;
  line-height: 80px;
  margin-right: 40px;
  cursor: pointer;
  display: none;
}
#check{
  display: none;
}
@media (max-width: 952px){
  label.logo{
    font-size: 27px;
    padding-left: 25px;
  }
  nav ul li a{
    font-size: 16px;
  }
}
@media (max-width: 858px){
  .checkbtn{
    display: block;
    margin-right: 40px;
  }
  ul{
    position: fixed;
    width: 100%;
    height: 100vh;
    background: black;
    top: 80px;
    left: -100%;
    text-align: center;
    transition: all .5s;
  }
  nav ul li{
    display: block;
    margin: 50px 0;
    line-height: 30px;
  }
  nav ul li a{
    font-size: 20px;
  }
  a:hover,a.active{
    background: none;
    color: black;
  }
  #check:checked ~ ul{
    left: 0;
  }
}
section{
  background-size: cover;
  height: calc(100vh - 80px);
}



.center{
  display: flex;
  text-align: center;
  align-items: center;
  justify-content: center;
}
.outer{
  position: relative;
  margin: 0 50px;
  background: black;
}
.button{
  height: 70px;
  width: 220px;
  border-radius: 50px;
}
.circle{
  height: 200px;
  width: 200px;
  border-radius: 50%;
}
.outer button, .outer span{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.outer button{
  background: black;
  color: #f2f2f2;
  outline: none;
  border: none;
  font-size: 20px;
  z-index: 9;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
}
.button button{
  height: 60px;
  width: 210px;
  border-radius: 50px;
}
.circle button{
  height: 180px;
  width: 180px;
  border-radius: 50%;
}
.outer span{
  height: 100%;
  width: 100%;
  background: inherit;
}
.button span{
  border-radius: 50px;
}
.circle span{
  border-radius: 50%;
}
.outer:hover span:nth-child(1){
  filter: blur(7px);
}
.outer:hover span:nth-child(2){
  filter: blur(14px);
}
.outer:hover{
  background: linear-gradient(#14ffe9, #ffeb3b, #ff00e0);
  animation: rotate 2.5s linear infinite;
}
@keyframes rotate {
  0%{
    filter: hue-rotate(0deg);
  }
  100%{
    filter: hue-rotate(360deg);
  }
}
@media (max-width: 640px){
  .center{
    flex-wrap: wrap;
    flex-direction: column;
  }
  .outer{
    margin: 50px 0;
  }
}
.logo{
  display: inline-flex;
  width: 180px;
  height: 55px;
  margin: 0 15px;
  perspective: 1000px;
}
.animate_bounce:hover {
  animation-name: bounce;
  animation-duration: 1.0s;
  animation-iteration-count: infinite;
}

body{
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.content{
  height: 100%;
  width: 100%;
  overflow: hidden;
  box-shadow: 1px 1px 15px rgba(0,0,0,0.4);
}
.content .images{
  height: 100%;
  width: 100%;
}
.images img{
  height: 100%;
  width: 100%;
}

Javascriptについてはそのままhtmlに記述しています。
スライドショーであればそこまで記述量が多くないですね。

前回前々回と、使用したWEBサイトを流用して使ってるため、リファクタリングしなきゃですね。
ここは間違ってる、ここはこうしたほうが良い等々ございましたらご指摘いただけますと幸いです。

最後までみていただきありがとうございます。

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?