JS Mini projects - Expanding Card

Seulingยท2022๋…„ 6์›” 13์ผ
0

FE

๋ชฉ๋ก ๋ณด๊ธฐ
8/42

๐Ÿ’ก ๊ตฌํ˜„ ์ „ ์ƒ๊ฐํ•ด๋ณด๊ธฐ

1๏ธโƒฃ card 5๊ฐœ, container1๊ฐœ ํ•„์š”ํ•˜์ง€์•Š์„๊นŒ? -> ๐Ÿ™†๐Ÿปโ€โ™€๏ธ
2๏ธโƒฃ 'click'์‹œ class ์ƒ์„ฑํ•ด์„œ css ๋ณ€๊ฒฝํ•˜๋ฉด๋˜์ง€์•Š์„๊นŒ? ๐Ÿ™†๐Ÿปโ€โ™€๏ธ

์ฒ˜์Œ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„
3๏ธโƒฃ class ์ƒ์„ฑ ํ›„ ๊ธฐ์กด์˜ active์˜€๋˜ class๋ฅผ ์ง€์›Œ์ฃผ๊ธฐ!! removeActiveClasses()

HTML

<!DOCTYPE html>
<html lang="ko">
  <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" />
    <title>My Project</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css"
      integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g=="
      crossorigin="anonymous"
      referrerpolicy="no-referrer"
    />
    <link rel="stylesheet" href="./style.css" />
  </head>
  <body>
    <div class="container">
      <div
        class="panel active"
        style="
          background-image: url('https://images.unsplash.com/photo-1511548774318-563182fe8d03?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTh8fGF8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60');
        "
      >
        <h3>AaAaAa</h3>
      </div>
      <div
        class="panel"
        style="
          background-image: url('https://images.unsplash.com/photo-1513569771920-c9e1d31714af?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Nnx8YmxhY2t8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60');
        "
      >
        <h3>BbBbBb</h3>
      </div>
      <div
        class="panel"
        style="
          background-image: url('https://images.unsplash.com/photo-1605392206257-e94842b83cae?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTJ8fGN8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60');
        "
      >
        <h3>CcCcCc</h3>
      </div>
      <div
        class="panel"
        style="
          background-image: url('https://images.unsplash.com/photo-1496150458551-140441714f2f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mzd8fGJsYWNrfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60');
        "
      >
        <h3>DdDdDd</h3>
      </div>
      <div
        class="panel"
        style="
          background-image: url('https://images.unsplash.com/photo-1497005367839-6e852de72767?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8OXx8ZSUyMGJsYWNrfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=500&q=60');
        "
      >
        <h3>EeEeEe</h3>
      </div>
    </div>
    <script src="./script.js"></script>
  </body>
</html>

CSS

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

* {
  box-sizing: border-box;
}

body {
  font-family: "Roboto", sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  overflow: hidden;

  margin: 0;
}

.container {
  display: flex;
  width: 90vw;
}

.panel {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 80vh;
  border-radius: 50px;
  color: #fff;
  cursor: pointer;
  flex: 0.5;
  margin: 10px;
  position: relative;
  transition: flex 0.7s ease-in;
}

.panel h3 {
  font-size: 24px;
  position: absolute;
  bottom: 20px;
  left: 20px;
  margin: 0;
  opacity: 0;
}

.panel.active {
  flex: 5;
}

.panel.active h3 {
  opacity: 1;
  transition: opacity 0.3s ease-in 0.4s;
}

@media (max-width: 480px) {
  .container {
    width: 100vw;
  }

  .panel:nth-of-type(4),
  .panel:nth-of-type(5) {
    display: none;
  }
}

JS

const panels = document.querySelectorAll(".panel");

panels.forEach((panel) => {
  panel.addEventListener("click", () => {
    removeActiveClasses();
    panel.classList.add("active"); //ํ™œ์„ฑ ํด๋ž˜์Šค ์ถ”๊ฐ€
  });
});

function removeActiveClasses() {
  panels.forEach((panel) => {
    panel.classList.remove("active");
  });
}
profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ํ•ญ์ƒ ๋ญ˜ ํ•˜๊ณ ์žˆ๋Š” ์Šฌ๋ง

0๊ฐœ์˜ ๋Œ“๊ธ€