๊ฐœ๋ฐœ ๐Ÿพ/HTML,CSS

ํƒญ ๊ตฌํ˜„ํ•˜๊ธฐ - html, css, jquery

JOTOKKI 2021. 2. 3. 19:30
728x90

 

๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํ•˜๋‹จ์˜ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋˜๋Š” ํƒญ์„ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

๋””์ž์ธ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ํ™œ์„ฑํ™”๋œ ๋ฒ„ํŠผ ๋ถ€๋ถ„์€ ํ•˜๋‚˜์˜ div๋กœ ์ปจํŠธ๋กค ํ•˜์—ฌ ํด๋ฆญ์‹œ ์œ„์น˜๋ฅผ ์ด๋™์‹œ์ผœ ์ข€ ๋” ์„ธ๋ จ(?)๋˜๊ฒŒ

๋””์ž์ธ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

 

 

 

1. HTML 

    <div class="wrap">
        <div class="tab_wrap">
            <div class="tab_buttons">
                <button class="active">๊ณต์ง€์‚ฌํ•ญ</button>
                <button>๋‰ด์Šค</button>
                <button>์ผ๋Œ€์ผ๋ฌธ์˜</button>
                <div class="button_bg"></div> <!-- ํ™œ์„ฑํ™”์‹œ ์ปจํŠธ๋กค ํ•  ๋ถ€๋ถ„ -->
            </div>
            <div class="tab_contents">
                <div class="tab_content active">
                    ๊ณต์ง€์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. <br>
                    ๊ณต์ง€์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. <br>
                    ๊ณต์ง€์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. <br>
                    ๊ณต์ง€์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. <br>
                    ๊ณต์ง€์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. 
                </div>
                <div class="tab_content">
                    News์ž…๋‹ˆ๋‹ค. <br>
                    News์ž…๋‹ˆ๋‹ค. <br>
                    News์ž…๋‹ˆ๋‹ค. <br>
                    News์ž…๋‹ˆ๋‹ค. <br>
                    News์ž…๋‹ˆ๋‹ค. 
                </div>
                <div class="tab_content">
                    1:1 ๋ฌธ์˜ ์ž…๋‹ˆ๋‹ค. <br>
                    1:1 ๋ฌธ์˜ ์ž…๋‹ˆ๋‹ค. <br>
                    1:1 ๋ฌธ์˜ ์ž…๋‹ˆ๋‹ค. <br>
                    1:1 ๋ฌธ์˜ ์ž…๋‹ˆ๋‹ค. <br>
                    1:1 ๋ฌธ์˜ ์ž…๋‹ˆ๋‹ค. 
                </div>
            </div>
        </div>
    </div>

 

2. CSS

* {
    padding:0;
    margin:0;
    box-sizing:border-box;
}

body{
    background-color:#eee;
}

.wrap {
    padding:30px;
}

.tab_buttons{
    position:relative;
    padding:12px 0;
    overflow:hidden;
}

.button_bg{
    position:absolute;
    top:50%;
    left:0;
    z-index:1;
    transform: translateY(-50%);
    display:inline-block;
    width:108px;
    height:45px;
    background: linear-gradient(#e66465, #9198e5);
    border-radius:45px;
}

.tab_buttons button {
    position:relative;
    z-index:2;
    float:left; 
    min-width:32px;
    height: 45px;
    padding:0 22px;
    border:0 none;
    background-color:transparent;
    font-size:16px;
    outline:none;
    cursor: pointer;
}

.tab_buttons button.active{
    color:#fff;
    font-weight:bold;
}

.tab_contents{
    position:relative;
}
.tab_content{
    position:absolute;
    top:0; 
    left:20px;
    z-index:1;
    padding:20px;
    transition: all 0.4s;
    opacity:0;
    color:#212121;
}

.tab_content.active{
    left:0;
    opacity:1;
}

 

๐Ÿ”น button ์— ํด๋ฆญ์ด๋ฒคํŠธ๋ฅผ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ button์ด button_bg๋ณด๋‹ค z-index๊ฐ€ ๋†’์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”น  tab_content ๋Š” ๊ตณ์ด absolute๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ, ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ํŽ˜์ด๋“œ์ธ ๋˜๋Š” ํšจ๊ณผ๋ฅผ ์ฃผ๊ณ  ์‹ถ์–ด์„œ absolute๋กœ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

 

3. JQuery

$(document).ready(function(){
    $('.tab_buttons button').click(function(){
        var activeBtn = $('.button_bg');
        var _this = $(this);
        var width = $(_this).outerWidth();  // button_bg ์—๋„ ์ ์šฉํ•  ๋™์ผํ•œ width๊ฐ’ ํ•„์š”
        var left = $(_this).position().left; // button_bg๊ฐ€ ์˜ฎ๊ฒจ๊ฐˆ ์œ„์น˜ ํ•„์š”
        var thisIndex = $(_this).index();  // ํด๋ฆญํ•œ ๊ฐ์ฒด์˜ ์ˆœ์„œ์— ๋”ฐ๋ผ ์ปจํ…์ธ ๋ฅผ ํ™œ์„ฑํ™”
        
        activeBtn.animate({
            width : width + 'px',
            left : left + 'px'
        }, 300, function(){
            $(_this).addClass('active').siblings().removeClass('active');
        });
        $('.tab_content').eq(thisIndex).addClass('active').siblings().removeClass('active');
        // button ์ˆœ์„œ์™€ ๋™์ผํ•œ tab_content ์— active๋ฅผ ์ฃผ์–ด ์ปจํ…์ธ  ํ™œ์„ฑํ™”, ๋‚˜๋จธ์ง€๋Š” active์‚ญ์ œ
    })
});

 

๋‹ค๋“ค jquery ๋งŽ์ด ์“ฐ์‹œ๋‚˜์š”? 

์š”์ฆ˜์€ ์ฃผ๋กœ javascript๋ฅผ ์“ฐ๊ธฐ๋•Œ๋ฌธ์— jquery๋Š” ์ •๋ง ์˜ค๋žซ๋งŒ์ด๋„ค์š”. ๐Ÿ˜…

 

๋ฐ˜์‘ํ˜•