Slider

Slider with flexbox

by Ravshanbek778

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <link rel="stylesheet" href="styles.css" />
  <title>Карточки | Проект 1</title>
</head>
<body>
<div class="counter"></div>
<div class="container">
  <div
      class="slide"
      style="background-image: url('https://images.unsplash.com/photo-1583121274602-3e2820c69888?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80');"
  >
    <h3>Ferrari</h3>
  </div>
  <div
      class="slide"
      style="
          background-image: url('https://images.unsplash.com/photo-1573950940509-d924ee3fd345?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1696&q=80');
        "
  >
    <h3>Lamborghini</h3>
  </div>
  <div
      class="slide"
      style="
          background-image: url('https://images.unsplash.com/photo-1492144534655-ae79c964c9d7?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1694&q=80');
        "
  >
    <h3>Camaro</h3>
  </div>
  <div
      class="slide"
      style="
          background-image: url('https://images.unsplash.com/photo-1535732820275-9ffd998cac22?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1650&q=80');
        "
  >
    <h3>Aston</h3>
  </div>
  <div
      class="slide"
      style="
          background-image: url('https://images.unsplash.com/photo-1554744512-d6c603f27c54?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1650&q=80');
        "
  >
    <h3>Tesla</h3>
  </div>
</div>
<script src="script.js"></script>
</body>
</html>

CSS

@import url('https://fonts.googleapis.com/css?family=Muli&display=swap'); 

* {
  box-sizing: border-box;
}

body {
  font-family: 'Muli', sans-serif;
  overflow: hidden;
  margin: 0;
  background: aliceblue;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.counter {
  position: absolute;
  top: 8px;
  left: 24px;
  font-family: 'Muli', sans-serif;
  font-size: 32px;
  font-weight: 700;
}
.container {
  width: 100%;
  display: flex;
  padding: 0 20px;
}
.slide {
  height: 80vh;
  border-radius: 20px;
  margin: 10px;
  color: #f5f5f5;
  cursor: pointer;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  flex: 1;
  transition: all .5s ease-in-out;
}
.slide h3 {
  position: absolute;
  font-size: 24px;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0;
  transition: .5s ease .5s;
}
.slide.active {
  flex: 10;
}
.slide.active h3 {
  opacity: 1;
}

JavaScript

function slidesPlugin(activeSlides){
      const slides = document.querySelectorAll('.slide');
   const counter = document.querySelector('.counter');
   
   slides[activeSlides].classList.add('active')

   slides.forEach((slide, index) => {
        slide.addEventListener('click', (e) => {
            clearActiveClasses();
            const target = e.target;
            let num = index + 1;
            let car = target.querySelector('h3');
            counter.textContent = `It is ${num} - picture: ${car.innerText}`;
            slide.classList.add('active')
        })
    })
    function clearActiveClasses() {
        slides.forEach(slide => {
            slide.classList.remove('active')
        })
    }
   }
   
    slidesPlugin(0);