프론트엔드/Javascript 연습과제 해답

브라우저 창 사이즈와 스크롤 연습과제 해답

syleemomo 2024. 2. 27. 18:41
728x90

연습과제 1

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="modal-container">
        <div class="modal">
            <header>안내사항</header>
            <div class="body">비오는날 조퇴나 지각을 자제하시고 몸관리 잘하셔서 출석하시기 바랍니다. 강사가 교체될수도 있습니다. 플리즈 ~~~</div>
            <footer>
                <button>확인</button>
                <button>취소</button>
            </footer>
        </div>
    </div>
    <script src="app.js"></script>
</body>
</html>
*{
    box-sizing: border-box;
}
body{
    margin: 0; padding: 0;
    width: 100%; height: 300vh;

    background: url(https://blog.kakaocdn.net/dn/ciUuSF/btrwA63osUr/qK4zOZpkmz3A8U9rMtkzO1/img.jpg) no-repeat center;
    background-size: cover;
}
.modal-container{
    position: fixed; 
    left: 0; top: 0; right: 0;
    z-index: 1;
    width: 100%; height: 100vh;

    display: none;
}
.modal{
    /* border: 1px solid red; */
    width: 30%; height: 300px; min-width: 300px;
    overflow: auto;
    border-radius: 5px;
    background-color: transparent;
    backdrop-filter: blur(17px);
    color: wheat; font-size: .8rem; font-weight: 500;
    padding: .5rem 1rem;
    box-shadow: 1rem 1rem 2rem rgba(0, 0, 0, .3);

    display: flex;
    flex-flow: column;
    justify-content: center;
    align-items: center;
    background-color: none;
}
.modal header{
    height: 40px; line-height: 40px;
    width: 100%;
    /* border: 1px solid red; */
    text-align: center;
    border-bottom: 1px solid wheat;
}
.modal .body{
    flex: 1;
    padding-top: 1rem;
    /* border: 1px solid green; */
}
.modal footer{
    border-top: 1px solid wheat;
    width: 100%;
    display: flex;
    justify-content: flex-end;
    padding-top: .5rem;
}
button{
    border: none; outline: none;
    padding: .3rem .5rem;
    border-radius: 5px;
    cursor: pointer;
    background: wheat; color:rgba(0, 0, 0, .6);
    font-size: .6rem; font-weight: 600;
    margin-left: .3rem;
}
.modal-container.show{
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 0, 0, .7);
}
const modalContainer = document.querySelector('.modal-container')
const controls = modalContainer.querySelectorAll('button')


function closeModal(){
    modalContainer.classList.remove('show')
    document.body.style.overflow = ''
}

setTimeout(function(){
    modalContainer.classList.add('show')
    document.body.style.overflow = 'hidden'
}, 1000)

for(let button of controls){
    button.addEventListener('click', closeModal)
}

 

연습과제 2

<!DOCTYPE html>
<html lang="en">
<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>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
        <div class="item">10</div>
    </div>
    <div class="controls">
        <button>prev</button>
        <button>next</button>
    </div>
    <script src="app.js"></script>
</body>
</html>
*{
    box-sizing: border-box;
}
body{
    margin: 0; padding: 0;
    width: 100%; min-height: 500vh;

    background: url(https://blog.kakaocdn.net/dn/ciUuSF/btrwA63osUr/qK4zOZpkmz3A8U9rMtkzO1/img.jpg) no-repeat center;
    background-size: cover;
}
.container{
    width: 60%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    margin: auto;
    margin-top: 50px;
    gap: 10px;
}
.container .item{
    background-color: transparent;
    width: 45%;
    height: 200px;
    backdrop-filter: blur(15px);
    border-radius: 15px;
    box-shadow: 1rem 1rem 2rem rgba(0, 0, 0, .7);
    font-size: 0;
    flex: 1 0 auto;
}
.controls{
    position: fixed;
    right: 2rem; bottom: 2rem;
}
.controls button{
    border: none; outline: none;
    padding: 1rem 2rem;
    border-radius: 5px;
    background-color: #0e1111;
    color: wheat;
    cursor: pointer;
    font-size: 1rem;
}
.controls button:hover{
    transform: scale(1.02);
    background-color: wheat;
    border: wheat;
    color: #0e1111;
}
const container = document.querySelector('.container')
const items = container.querySelectorAll('.item')
const controls = document.querySelector('.controls')
let index = 0

function moveItems(e){
    if(e.target.innerText === 'prev'){
        console.log('이전')
        index--
        if(index < 0){
            index = 0
        }
    }else if(e.target.innerText === 'next'){
        console.log('다음')
        index++
        if(index > parseInt(items.length / 2)){
            index = parseInt(items.length / 2)
        }
    }
    console.log(index, index*2-1)

    const items2move = items[index*2-1]
    if(items2move){
        items2move.scrollIntoView(true)
    }
}


controls.addEventListener('click', moveItems)
728x90