✿∘˚˳°∘°

45일차 : [실습] 상품상세페이지만들기 본문

국비수업/JavaScript

45일차 : [실습] 상품상세페이지만들기

_HYE_ 2023. 2. 2. 10:48

20230131

메인화면
상품정보 - 더보기를하면 감춰진 내용 표시
다른탭을 클릭하면 내용이 바뀐다 - 1
다른 탭을 클릭하면 내용이 바뀐다 - 2

 

<!DOCTYPE html>
<html lang="ko">
<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>[해설]KH-SHOP</title>
    <link rel="stylesheet" href="css/khshop1.css">
</head>
<body>
    <div class="wrapper">
        <div class="header">
            <h1>KH-SHOP</h1>
        </div>
        <div class="content">
            <div class="content-title">
                <h3>ICECREAM</h3>
            </div>
            <div class="content-main">
                <div class="product-img">
                    <image src="icemall-image/ice4.png"></image>
                </div>
                <div class="product-preview">
                    <div class="product-title">
                        <h2>사랑에 빠진 딸기</h4>
                    </div>
                    <div class="product-info">
                        <table>
                            <tr>
                                <th>구성</th>
                                <td>[베스킨라빈스] 싱글컵</td>
                            </tr>
                            <tr>
                                <th>가격</th>
                                <td>3,000원</td>
                            </tr>
                            <tr>
                                <th>적립포인트</th>
                                <td>0P</td>
                            </tr>
                        </table>
                    </div>
                    <div class="buy-btn">
                        <button class="gift">선물하기</button>
                        <button class="cart">장바구니</button>
                    </div>
                </div>
            </div>
            <div class="content-detail">
                <div class="tab">
                    <ul>
                        <li>상품정보</li>
                        <li>이용안내</li>
                        <li>주의사항</li>
                    </ul>
                </div>
                <div class="tab-content">
                    <div class="product-detail">
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <p>상품상세</p>
                        <button id="more-btn">더보기</button>
                    </div>
                    <div>
                        <p>이용안내</p>
                        <p>이용안내</p>
                        <p>이용안내</p>
                        <p>이용안내</p>
                        <p>이용안내</p>
                        <p>이용안내</p>
                    </div>
                    <div>
                        <p>주의사항</p>
                        <p>주의사항</p>
                        <p>주의사항</p>
                        <p>주의사항</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="js/khshop1.js"></script>
</body>
</html>
*{
    margin: 0;
    padding: 0;
    outline: none;
}
.header>h1{
    background: #d9a7c7;  /* fallback for old browsers */
    background: -webkit-linear-gradient(to right, #fffcdc, pink);  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to left, #fffcdc, pink); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    font-size: 30px;
    color: white;
    padding: 10px 20px;
}

.content{
    width: 1200px;
    margin: 0 auto;
    padding-top: 30px;
}

.content-title>h3{
    text-align: center;
    padding: 20px 0px;
    border-bottom: 2px solid #d9a7c7;
}

.content-main{
    overflow: hidden;
}
.product-img{
    float: left;
    width: 500px;
    box-sizing: border-box;
    padding: 30px 50px;
}

.product-preview{
    float: left;
    width: 700px;
    padding-top:50px;
}

.product-title>h2{
    padding: 20px;
}

.product-info>table{
    width: 100%;
    border-top: 1px solid #d9a7c7;
    border-bottom: 1px solid #d9a7c7;
}

.product-info>table th{
    padding: 20px;
    text-align: left;
}

.buy-btn{
    padding: 20px;
}

.buy-btn>button{
    border: none;
    width: 150px;
    height: 50px;
    cursor: pointer;
    color: white;
    font-size: 18px;
    font-weight: bold;
    border-radius: 2px;
}

.gift{
    background-color: pink;
}
.cart{
    background-color: pink;
}

.tab>ul{
    list-style-type: none;
    overflow: hidden;
}
.tab>ul>li{
    width: calc(100%/3);
    float: left;
    height: 50px;
    line-height: 50px;
    text-align: center;
    border: 1px solid pink;
    box-sizing: border-box;
    color: #ccc;
    cursor: pointer;
}

.tab>ul>li.active-tab{
    /*클래스보다 클래스의 자손이 우선순위가 높으므로 
    클래스만(.active-tab) 사용되면 위코드(.tab>ul>li)때문에 
    겹치는 스타일은 적용이 안됨*/
    border-top: 3px solid pink;
    color: #d9a7c7;
    font-weight: bold;
    border-bottom: none;
}

.tab-content>div{
    padding: 50px 30px;
    box-sizing: border-box;
    display: none;
}

.tab-content>div.active-content{
    display: block;
}

.product-detail{
    height: 200px;
    overflow: hidden;
    position: relative;
}

#more-btn{
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 50px;
    border: none;
    font-size: 18px;
    color: #fff;
    background-color: pink;
    cursor: pointer;
    font-weight: bold;
}
const tabs = document.querySelectorAll(".tab>ul>li");
tabs.forEach(function(tab, index){
    tab.addEventListener("click", function(){
    //tab == tabs[0] tabs[1] tabs[2]
        tabs.forEach(function(removeTab){
            removeTab.classList.remove("active-tab");
        });
        //클릭한 li에만 active-tab클래스 추가
        tab.classList.add("active-tab");
        const contents = document.querySelectorAll(".tab-content>div");
        contents.forEach(function(content){
            content.classList.remove("active-content");
        });
        //클릭한 li와 같은 index번호를 가진 div에만 active-content추가
        contents[index].classList.add("active-content");
    });
});

//html다 만든후 실행
window.onload = function(){
    //페이지 로드가 완료되면 페이지를 정상화하기위한 기본작업
    //(들어가자마자 상품정보탭이 눌려있도록) : 첫번째 li와 첫번째 div에 클래스추가
    //첫번째 탭, 첫번째 컨텐츠를 활성화
    const activeLi = document.querySelector(".tab>ul>li:first-child");
    const activeContent = document.querySelector(".tab-content>div:first-child");
    activeLi.classList.add("active-tab");
    activeContent.classList.add("active-content");
}

const moreBtn = document.querySelector("#more-btn");

moreBtn.addEventListener("click", function(){
    const productDetail = document.querySelector(".product-detail");
    productDetail.style.height = "auto"; //자식높이만큼
    // productDetail.style.overflow = "visible";
    // overflow="visible";을 사용하면 안되는 이유
    // 배경색이 있을경우 영역자체는 그대로이기 때문에 흰색으로 들어가진다
    this.remove();
});

 

[ 후기 ]

팀프로젝트를 할 때 탭을넘기면 내용이 표시되는걸 어떻게 해야할지 고민하다가 못하고 제출했는데

자바스크립트를 사용하니까 너무간단하게 되어서 신기하고 재미있었다. 

'국비수업 > JavaScript' 카테고리의 다른 글

46일차 : [실습] 단계형 회원가입 만들기  (0) 2023.02.02
44일차 : EVENT  (0) 2023.01.30
43일차 : [실습]DOM / BOM  (0) 2023.01.30
42일차 : DOM  (0) 2023.01.26
41일차 : JavaScript의 기본  (0) 2023.01.25
Comments