๊ฐœ๋ฐœ ๐Ÿพ/Playground

[html, css, javascript] ์‹ฌ์‹ฌํ•ด์„œ ๋งŒ๋“ค์–ด๋ณธ ๋ฉ”์ธ ํ™”๋ฉด ํšจ๊ณผ

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

๋ฉ‹์ง„ ํ™ˆํŽ˜์ด์ง€๋“ค์„ ํƒ์ƒ‰ํ•˜๋˜ ์ค‘ ๊ดœ์ฐฎ์•„ ๋ณด์ด๋Š” ํšจ๊ณผ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

๊ตฌํ˜„๋œ ํ™”๋ฉด

๋‹ค์Œ๊ณผ ๊ฐ™์€ ํšจ๊ณผ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์กฐ์™€ ๋ฐฉ์‹์€ ๋‹ค์–‘ํ•˜๊ฒ ์ง€๋งŒ, 

์ €๋Š” ๋‹จ์ˆœํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

 

div ์•ˆ์— inner_div๋ฅผ ๋‘๊ณ  

์ฒซ๋ฒˆ์งธ์ค„์˜ inner_div์—๊ฒŒ๋Š” left -100% ๋ฅผ ๋‘๋ฒˆ์งธ ์ค„์˜ inner_div์—๊ฒŒ๋Š” left 100%๋ฅผ ์ฃผ์–ด,

๋™์‹œ์— left: 0์ด ๋ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์•ก์…˜์ด ์ผ์–ด๋‚˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

 

classํ•˜๋‚˜๋กœ ์ปจํŠธ๋กค ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— javascript์€ ๋‹จ์ˆœํžˆ class๋งŒ ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

html ๊ตฌ์กฐ

1. html

<div class="wrapper" >
    <button class="button">Click Me</button>
    <div class="container" style="background-image:url('./bg.jpg')">
    </div>

    <!-- divider s -->
    <div class="divider">
        <div class="row to_left">
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
        </div>
        <div class="row to_right">
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
            <div class="block"><div class="inner-block"></div></div>
        </div>
    </div>
    <!-- divider e -->
</div>

2. css

html,body{
    width:100%;
    height:100%;
}

* {
    padding:0;
    margin:0;
}

.wrapper{
    position: relative;
    width:100%;
    height:100%;
}

.container{
    width:100%;
    height:100%;
    background-size: cover;
}

.button{
    position: fixed;
    top:20px;
    right:20px;
    z-index:100;
    padding:20px;
    font-size: 15px;
}

.divider{
    position: fixed;
    top:0;
    left:0;
    width: 100%;
    height:100%;
    z-index:10;
    background:rgba(0,0,0,0.4);
    display: flex;
    flex-direction: column;    
}

.divider .row{
    flex: 1;
    display:flex;
}

.divider .row:first-child{
    border-bottom: 1px solid rgba(0,0,0,0.4);
}

.divider .row .block{
    position: relative;
    overflow:hidden;
    border-right:1px solid rgba(0,0,0,0.4);
}


.divider .row .block:nth-child(1),
.divider .row .block:nth-child(4){
    flex: 1;
}

.divider .row .block:nth-child(2),
.divider .row .block:nth-child(3){
    flex: 2;

}

.divider .row .inner-block{
    position: absolute;
    top:0;
    left:0;
    z-index: 1;
    width:100%;
    height: 100%;
    background-color:lightgrey;
    transition: left 0.3s;
}

.divider .row.to_left .inner-block{  /* ์ฒซ ๋ฒˆ์งธ ์ค„ left -> right */
    left:-100%;
}

.divider .row.to_right .inner-block{ /* ๋‘ ๋ฒˆ์งธ ์ค„ right -> left */
    left:100%;
}

.divider .row.to_right.active .inner-block,
.divider .row.to_left.active .inner-block{
    left:0;
}



3. script

var btn = document.querySelector('.button');
var rows = document.querySelectorAll('.row');

btn.addEventListener('click', function(){
if(rows[0].classList.contains('active')){
    for(var i=0; i< rows.length; i++){
        rows[i].classList.remove('active');            
    }    
} else {
    for(var i=0; i< rows.length; i++){
        rows[i].classList.add('active');            
    }    
}
});

 

์‹ฌ์‹ฌํ•ด์„œ ๋งŒ๋“ค์–ด ๋ณธ ํ™”๋ฉด์ด์ง€๋งŒ ์–ด๋””์— ์“ธ ๋ฐ๊ฐ€ ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š”. ๐Ÿ˜‚

 

๋ฐ˜์‘ํ˜•