GalleryTutorials

How to create Simple Image Slider

How to create Simple Image Slider

How to create Simple Image Slider

Before We Start

A gallery is one of the main components of a website, It contains links to the other sections and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website.

How to create Simple Image Slider. So if you are a person who wishes to develop a website, you have to know how to create a gallery properly.

How to create Simple Image Slider ? We are here to solve your problem.
In this article we discuss how to create a gallery. Before we start please read the below articles which easier you to understand the process, if you are new to development.

Step 1 – Add HTML

It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.

<body onload="loading()">
  <div class="container">
    <div class="box">
    </div>
    <i class="fa fa-arrow-circle-o-left btn btn-left"></i>
    <i class="fa fa-arrow-circle-o-right btn btn-right"></i>
    <div class="image-gallery">
      <img src="https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975098/sh_5.jpg" alt="shoe" class="image">
    </div>
    <!-- -----------------------------------SCRIPT HERE -->
    <script src="./js/app.js"></script>
    <script src="https://kit.fontawesome.com/a844e5ba96.js" crossorigin="anonymous"></script>
</body>

Step 2 – Add CSS

Copy and paste below code in your HTML editor between <style></style> tag.

<style>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: montserrat, sans-serif;
}

body {
  background-color: rgb(22, 22, 24);
}
/* .box {
            background-color: rgb(22, 22, 24);
            padding: 1rem;
            width: 40px;
        } */

.container {
  max-width: 700px;
  margin: auto;
}

.header {
  text-align: center;
  margin-left: -100px;
  color: white;
  padding-top: 2rem;
}

.author {
  font-family: cursive;
  font-weight: 500;
  font-size: 1.7rem;
  text-align: center;
  margin-top: 4rem;
  color: #fff;
  font-family: "Playball", cursive;
}
.author-name {
  font-family: "Faster One", cursive;
}

.image-gallery {
  padding: 2rem;
  background-color: white;
  margin-top: 2rem;
  max-width: 600px;
}

.image-gallery .image {
  display: block;
  width: 100%;
  margin: auto;
}

.btn {
  display: inline-block;
  z-index: 99999;
  color: rgb(255, 255, 255);
  font-size: 1.7rem;
}

.btn-right {
  transform: translate(1800%, 900%);
}

.btn-left {
  transform: translate(200%, 900%);
}

@media (max-width: 750px) {
  .btn-right {
    transform: translate(1400%, 750%);
  }
  .btn-left {
    transform: translate(200%, 750%);
  }
}

@media (max-width: 600px) {
  .btn-right {
    transform: translate(1750%, 750%);
  }
  .btn-left {
    transform: translate(200%, 750%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 550px) {
  .btn-right {
    transform: translate(1450%, 750%);
  }
  .btn-left {
    transform: translate(200%, 750%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 500px) {
  .btn-right {
    transform: translate(1600%, 750%);
  }
  .btn-left {
    transform: translate(200%, 750%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 450px) {
  .btn-right {
    transform: translate(1200%, 650%);
  }
  .btn-left {
    transform: translate(200%, 650%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 400px) {
  .btn-right {
    transform: translate(1150%, 650%);
  }
  .btn-left {
    transform: translate(200%, 650%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}

@media (max-width: 350px) {
  .btn-right {
    transform: translate(1000%, 650%);
  }
  .btn-left {
    transform: translate(200%, 650%);
  }
  .header {
    text-align: center;
    margin-left: 0;
  }
}
</style>

Step 2 – Add JavaScript

Copy and paste below code in your HTML editor between <script></script> tag.

<script>

let slider = [
  "https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975098/sh_5.jpg",
  "https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975100/sh_2.jpg",
  "https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975110/sh_3.jpg",
  "https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975107/sh_4.jpg",
  "https://res.cloudinary.com/dnuoffzlr/image/upload/v1603975109/sh_1.jpg"
];
let img = document.querySelector(".image");
let bgSet = document.body;

let rightBtn = document.querySelector(".btn-right");
let leftBtn = document.querySelector(".btn-left");
var i = 0;
rightBtn.addEventListener("click", function (e) {
  i++;
  if (i >= slider.length) {
    i = 0;
  }
  img.setAttribute("src", slider[i]);
  switch (i) {
    case 0:
      document.querySelector(".image-gallery").style.backgroundColor = "white";
      break;
    case 1:
      document.querySelector(".image-gallery").style.backgroundColor =
        "slateBlue";
      break;
    case 2:
      document.querySelector(".image-gallery").style.backgroundColor =
        "rgb(235, 185, 78)";
      break;
    case 3:
      document.querySelector(".image-gallery").style.backgroundColor = "black";
      break;
    case 4:
      document.querySelector(".image-gallery").style.backgroundColor =
        "#17cea6";
      break;
    default:
      document.querySelector(".image-gallery").style.backgroundColor =
        "rgb(235, 185, 78)";
  }
});
leftBtn.addEventListener("click", function (e) {
  i--;
  if (i < 0) {
    i = slider.length - 1;
  }
  img.setAttribute("src", slider[i]);
  switch (i) {
    case 0:
      document.querySelector(".image-gallery").style.backgroundColor = "white";
      break;
    case 1:
      document.querySelector(".image-gallery").style.backgroundColor =
        "slateBlue";
      break;
    case 2:
      document.querySelector(".image-gallery").style.backgroundColor =
        "rgb(235, 185, 78)";
      break;
    case 3:
      document.querySelector(".image-gallery").style.backgroundColor = "black";
      break;
    case 4:
      document.querySelector(".image-gallery").style.backgroundColor =
        "#17cea6";
      break;
    default:
      document.querySelector(".image-gallery").style.backgroundColor =
        "rgb(235, 185, 78)";
  }
});

function loading() {
  alert(
    "You can change the background color of this page simply by click anywhere in the page"
  );
}
bgSet.addEventListener("mousemove", function (e) {
  let cordY = e.offsetY;
  let cordX = e.offsetX;
  bgSet.style.backgroundColor = `rgb(${cordX}, ${cordY}, 40)`;
});
</script>

At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.

How to create Simple Image Slider

Video Tutorial

Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.


Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button