IT/Front

[HTML/CSS/JAVASCRIPT/jQuery] 버튼 클릭 이미지 슬라이드

Huitopia 2024. 2. 14. 20:37
728x90

<!-- index.html -->
<article id="at1" class="at">
    <div class="title title1"></div>
    <div class="at1-box">
      <ul class="at1-img">
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
    <ul class="at1-btn">
      <li class="prev">&lt;</li>
      <li class="disp">1 / 4</li>
      <li class="next">&gt;</li>
    </ul>
  </article>
// index.js
$(function () {
  //-- at1 btn img slide
  let cnt = 0;
  $(".at1-btn li").on("click", function () {
    let at1No = $(this).index();
    if (at1No == 2 && cnt < 3) {
      cnt++;
      $(".at1-img").animate({ left: "-=100%" }, 500);
    } else if (at1No == 0 && cnt > 0) {
      cnt--;
      $(".at1-img").animate({ left: "+=100%" }, 500);
    }
    $(".disp").text(cnt + 1 + " / 4");
  });
});
/* index.css */
/* article 1 */
.at1-box {
  border: 1px solid black;
  width: 500px;
  height: 400px;
  position: relative;
  top: 0;
  left: 0;
  overflow: hidden;
}
.at1-box .at1-img {
  border: 1px solid blue;
  width: 2000px;
  height: 100%;
  display: flex;
  position: absolute;
  top: 0;
  left: 0;
}
.at1-box .at1-img li {
  border: 1px solid black;
  width: 500px;
  height: 100%;
}

.at1-btn {
  /* border: 1px solid black; */
  width: 500px;
  height: 30px;
  display: flex;
  justify-content: space-between;
}
.at1-btn li {
  width: 20%;
  height: 100%;
  /* border: 1px solid blue; */
  text-align: center;
  line-height: 30px;
  cursor: pointer;
}

결과물

 

마무리는 overflow: hidden; 으로 영역 밖 이미지 숨김처리

 

728x90