🎨 background-slider

kirin.log·2021년 4월 11일
0

🪐 logic

  • html로 image와 button을 생성한다
  • css를 통해 styling
    • slide의 기본 opacity는 0으로 설정한 뒤, active 되면 opacity가 1이 되도록 한다
  • 이미지 js기능 추가 ➡ 보여지는 activeSlide를 기본 0으로 설정
  • 버튼을 누르면 slide갯수가 추가(삭제)되도록 한다
  • 추가 될 경우, 보여지는 activeSlide의 값이 전체 slide갯수보다 커지면 0이 되도록
  • 제거 될 경우, 보여지는 activeSlide의 값이 0보다 작아지면 전체 slide갯수가 되도록
  • css에서 opacity를 기본 0으로, active될 경우 1로 설정해 놓았기 때문에 classList를 활용하여
    class를 추가(opacity: 1) 및 제거(opacity: 0)되도록 하는 함수를 설정하여 각 버튼에 실행되도록 한다
🐸 html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="index.css" />
    <title>New Year Countdown</title>
  </head>
  <body>
    <div class="slider-container">
        <div class="slide active" style="
            background-image: url('https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
          ">
        </div>
        <div class="slide" style="
            background-image: url('https://images.unsplash.com/photo-1511593358241-7eea1f3c84e5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1934&q=80');
          ">
        </div>
        <div class="slide" style="
            background-image: url('https://images.unsplash.com/photo-1495467033336-2effd8753d51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
          ">
        </div>
        <div class="slide" style="
            background-image: url('https://images.unsplash.com/photo-1522735338363-cc7313be0ae0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2689&q=80');
          ">
        </div>
        <div class="slide" style="
            background-image: url('https://images.unsplash.com/photo-1559087867-ce4c91325525?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2100&q=80');
          ">
        </div>

        <button class="arrow left-arrow" id="left">
            <i class="fas fa-arrow-left"></i>
        </button>

        <button class="arrow right-arrow" id="right">
            <i class="fas fa-arrow-right"></i>
        </button>

    <script src="index.js"></script>
   
</body>
</html>
🐸 css

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');

* {
  box-sizing: border-box;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
    font-family: 'Roboto', sans-serif;
    background-position: center center;
    background-size: cover;
    transition: 0.4s;
  }

  /* 전체 흑백 투명 배경을 깔아준다 */
  body::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: -1;
  }

  /* slide img가 들어올 slider-container자리를 만들어준다 */
  .slider-container {
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.5);
    height: 70vh;
    width: 70vw;
    position: relative;
    overflow: hidden;
  }

  .slide {
    position: absolute;
    top: -15vh;
    left: -15vw;
    height: 100vh;
    width: 100vw;
    background-position: center center;
    background-size: cover;
    transition: 0.4s ease;
    z-index: 1;
    opacity: 0;
  }
 
  .slide.active {
    opacity: 1;
  }

  /* button */
  .arrow {
    position: fixed;
    background-color: transparent;
    color: #fff;
    padding: 20px;
    font-size: 30px;
    border: 2px solid orange;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
  }
 
  .arrow:focus {
    outline: 0;
  }
 
  .left-arrow {
    left: calc(15vw - 65px);
  }
 
  .right-arrow {
    right: calc(15vw - 65px);
  }
🐸 js

const body = document.body
const slides = document.querySelectorAll('.slide')
const leftBtn = document.getElementById('left')
const rightBtn = document.getElementById('right')

let activeSlide = 0

rightBtn.addEventListener('click', () => {
    activeSlide++

    if (activeSlide > slides.length - 1) {
        activeSlide = 0
    }

    setBgToBody()
    setActiveSlide()
})

leftBtn.addEventListener('click', () => {
    activeSlide--
.
    if (activeSlide < 0) {
        activeSlide = slides.length - 1
    }

    setBgToBody()
    setActiveSlide()
})

setBgToBody()

function setBgToBody() {
    body.style.backgroundImage = slides[activeSlide].style.backgroundImage
}

function setActiveSlide() {
    slides.forEach((slide) => slide.classList.remove('active'))

    slides[activeSlide].classList.add('active')
}
profile
boma91@gmail.com

0개의 댓글