๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/JavaScript jQuery

[JavaScript / jQuery] ์บ๋Ÿฌ์…€(์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ) ๋งŒ๋“ค๊ธฐ

by Connecting-the-dots 2022. 3. 5.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • ์˜ค๋Š˜์€ ์ด๋ฏธ์ง€ ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์•˜๋‹ค.
  • ํ™”๋ฉด ํ•˜๋‹จ ๊ฐ€์šด๋ฐ์— ์žˆ๋Š” ๋™๊ทธ๋ผ๋ฏธ ๋ฒ„ํŠผ 3๊ฐœ๋Š” ๊ฐ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํ•ด๋‹นํ•˜๋Š” ์ˆœ์„œ์˜ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™์‹œ์ผœ์ค€๋‹ค.
  • ๋™๊ทธ๋ผ๋ฏธ ๋ฒ„ํŠผ๋“ค ์˜†์— ํ™”์‚ดํ‘œ๋Š” ์ด์ „, ๋‹ค์Œ ๋ฒ„ํŠผ์œผ๋กœ ํ˜„์žฌ ์Šฌ๋ผ์ด๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ด์ „ ์Šฌ๋ผ์ด๋“œ๋‚˜ ๋‹ค์Œ ์Šฌ๋ผ์ด๋“œ๋กœ ์ด๋™์‹œ์ผœ์ค€๋‹ค.
  • ์‚ฌ์‹ค ๋™๊ทธ๋ผ๋ฏธ ๋ฒ„ํŠผ 3๊ฐœ๋Š” ์•„๋ž˜์ชฝ์—, ํ™”์‚ดํ‘œ ๋ฒ„ํŠผ์€ ์Šฌ๋ผ์ด๋“œ ์–‘ ๋์— ๋„ฃ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ position: absolute ๋„ z-index ๋ฅผ ์ด์šฉํ•ด๋„ ์›ํ•˜๋Š” ๋ฐฐ์น˜๊ฐ€ ์ž˜ ๋‚˜์˜ค์ง€ ์•Š์•˜๊ณ , ์–ด์ฐŒ์–ด์ฐŒ ๋‚˜์˜ค๋ฉด ๋ฐ˜์‘ํ˜•์—์„œ ์˜ˆ์˜๊ฒŒ ๋ฐฐ์น˜๋˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜๋„ ๊ตณ์ด ๋งŒ๋“ค๋ ค๋ฉด media query ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์€๋ฐ... ๋‹ค์Œ๋ฒˆ์— ์ข€ ๋” ๊ณต๋ถ€ํ•ด์„œ ๋‹ค์‹œ ๋„์ „ํ•ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿ’œ HTML

<div style="overflow: hidden;">
    <div class="slide-container">
        <div class="slide-box">
            <img src="./img/car1.png">
        </div>
        <div class="slide-box">
            <img src="./img/car2.png">
        </div>
        <div class="slide-box">
            <img src="./img/car3.png">
        </div>
    </div>
    <div class="btns">
        <button class="angle-btn prev">
            <i class="fas fa-angle-left"></i>
        </button>
        <button class="dot-btn slide-1"> </button>
        <button class="dot-btn slide-2"> </button>
        <button class="dot-btn slide-3"> </button>
        <button class="angle-btn next">
            <i class="fas fa-angle-right"></i>
        </button>
    </div>
</div>

๐Ÿ’œ CSS

.slide-container {
  width: 300vw;
  transition: all 1s;
}

.slide-box {
  width: 100vw;
  float: left;
}

.slide-box img {
  width: 100%;
}

.btns {
  display: inline-block; /* margin-collapse ํ•ด๊ฒฐ */
  width: 100%; /* margin-collapse ํ•ด๊ฒฐ */
  text-align: center;
  padding: 10px;
  z-index: 5;
  top: -70px;
  position: relative;
}

.angle-btn {
  background: transparent;
  border: none;
  font-size: 30px;
  color: gold;
}

.angle-btn:active {
  color: darkgoldenrod;
}

.prev {
  margin-right: 20px;
}

.next {
  margin-left: 20px;
}

.dot-btn {
  background: gold;
  border: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  transform: translate(0, -55%);
  margin: 0 5px;
}

.dot-btn:focus {
  background: darkgoldenrod;
}
  • ์ผ๋‹จ ๋‚ด๊ฐ€ ๋ณด๋Š” ํ™”๋ฉด์˜ ๋„ˆ๋น„๋Š” 100vw ์ด๊ณ  ์Šฌ๋ผ์ด๋“œ ์ด๋ฏธ์ง€๋Š” float ์†์„ฑ์ด ์ ์šฉ๋˜์–ด ์ด 3์žฅ์ด ๊ฐ€๋กœ๋กœ ๋†“์ธ ์ƒํƒœ์ด๋ฏ€๋กœ ์ „์ฒด ๋„ˆ๋น„๋Š” 300vw ๋กœ ์„ค์ •ํ–ˆ๋‹ค.
  • ์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด ๊ฐ€๋กœ๋กœ ์Šคํฌ๋กค์ด ๊ธธ๊ฒŒ ์ƒ๊ธฐ๋Š”๋ฐ ์ผ๋‹จ ์Šคํฌ๋กค ์—†์ด ์ฒซ ์ด๋ฏธ์ง€๋งŒ ๋ณด์—ฌ์•ผ ํ•˜๋ฏ€๋กœ, overflow: hidden ์†์„ฑ์„ ์ด์šฉํ•ด์„œ ์Šคํฌ๋กค์„ ์—†์•ด๋‹ค.
  • ์‹ค์Šต์„ ํ•˜๋‹ค๊ฐ€ ๋ฒ„ํŠผ ๋ฐฐ์น˜ ๊ณผ์ • ์ค‘์— margin-collapse ํ˜„์ƒ์„ ๋‹ค์‹œ ๋งˆ์ฃผํ–ˆ๋‹ค. 
  • ์—ด์‹ฌํžˆ ๊ตฌ๊ธ€๋งํ•œ ๊ฒฐ๊ณผ display: inline-block ์œผ๋กœ ํ•˜๊ณ  width ๋ฅผ 100% ๋กœ ์„ค์ •ํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋œ๋Œ€์„œ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.

๐Ÿ’œ JavaScript

var slide = 1;
$('.slide-1').on('click', function(){
    $('.slide-container').css('transform', 'translateX(0vw)');
    slide = 1;
})

$('.slide-2').on('click', function(){
    $('.slide-container').css('transform', 'translateX(-100vw)');
    slide = 2;
})

$('.slide-3').on('click', function(){
    $('.slide-container').css('transform', 'translateX(-200vw)');
    slide = 3;
})

$('.prev').on('click', function(){
    if (slide == 3){
        $('.slide-container').css('transform', 'translateX(-100vw)');
        slide = 2;
    } else if (slide == 2){
        $('.slide-container').css('transform', 'translateX(-0vw)');
        slide = 1;
    }
})

$('.next').on('click', function(){
    if (slide == 1){
        $('.slide-container').css('transform', 'translateX(-100vw)');
        slide = 2;
    } else if (slide == 2){
        $('.slide-container').css('transform', 'translateX(-200vw)');
        slide = 3;
    }
})
  • ์Šฌ๋ผ์ด๋“œ๋ฅผ ๋ฒ„ํŠผ์œผ๋กœ ์กฐ์ž‘ํ•˜์—ฌ ์›€์ง์ด๋ ค๋ฉด ํ˜„์žฌ ๋‚ด๊ฐ€ ๋ณด๋Š” ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๋ช‡ ๋ฒˆ์งธ์ธ์ง€ ๊ธฐ์ค€์ด ํ•„์š”ํ•˜๋‹ค.
  • ์šฐ์„  ์ฒซ ์ด๋ฏธ์ง€๋ฅผ 1๋ฒˆ์œผ๋กœ ์ •ํ•˜๊ณ , ๋‘๋ฒˆ์งธ ๋™๊ทธ๋ผ๋ฏธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ 2๋ฒˆ ์Šฌ๋ผ์ด๋“œ๋กœ, ์„ธ๋ฒˆ์งธ ๋™๊ทธ๋ผ๋ฏธ ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ 3๋ฒˆ ์Šฌ๋ผ์ด๋“œ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์Šฌ๋ผ์ด๋“œ๋ฅผ ์˜ฎ๊ธด ํ›„์—๋Š” ํ˜„์žฌ ๋‚ด๊ฐ€ ๋ณด๋Š” ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ, ํ•ด๋‹นํ•˜๋Š” ์Šฌ๋ผ์ด๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์ด์ „, ๋‹ค์Œ ๋ฒ„ํŠผ์˜ ๊ฒฝ์šฐ ๋‚ด๊ฐ€ ํŠน์ • ์Šฌ๋ผ์ด๋“œ์—์„œ ์ด์ „ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์–ด๋–ค ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๋‚˜์˜ค๊ณ , ๋‹ค์Œ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์–ด๋–ค ์Šฌ๋ผ์ด๋“œ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ์œ„์น˜๋ฅผ ์žก์•„์ฃผ๊ณ , ํ•ด๋‹น ์œ„์น˜์˜ ์Šฌ๋ผ์ด๋“œ ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•