[UI 개발] css3 - tab menu 제작(간단한 탭메뉴 제작)

남양주개발자

·

2016. 10. 18. 22:26

728x90
반응형

 

🖐 들어가기 전에

이번에는 간단한 탭메뉴를 자바스크립트를 통해 구현한 코드를 공유하도록 하겠습니다.

아래에 예시처럼 동작하는 UI를 개발하는 것이 이번 포스팅의 목표입니다.

💻 HTML 시멘틱마크업

<div id="container">
    <h2>How to develop a tab menu with jQuery</h2>
    <p>제작자 : 박경두</p>
    <p>고려대학교 경영정보학과</p>
    <p>멋쟁이 사자처럼 4기</p>
    <ul class="tab">
        <li class="current" data-tab="tab1"><a href="#">About</a></li>
        <li data-tab="tab2"><a href="#">Portfolio</a></li>
        <li data-tab="tab3"><a href="#">Contact</a></li>
        <li data-tab="tab4"><a href="#">Travel</a></li>
    </ul>

    <div id="tab1" class="tabcontent current">
        <h3>About</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>

    <div id="tab2" class="tabcontent">
        <h3>Portfolio</h3>
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
    </div>

    <div id="tab3" class="tabcontent">
        <h3>Contact</h3>
        <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
    </div>

    <div id="tab4" class="tabcontent">
        <h3>Travel</h3>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
</div>

우선 탭메뉴를 제작하기 위한 기본적인 HTML 마크업을 위 코드와 같이 진행했습니다.

💻 CSS3

<style>
    #container {
        width:960px;
        margin:0 auto;
        text-align:center;
    }
    .tab {
        list-style: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }
    /* Float the list items side by side */
    .tab li {
        float: left;
    }
    /* Style the links inside the list items */
    .tab li a {
        display: inline-block;
        color: #000;
        text-align: center;
        text-decoration: none;
        padding: 14px 16px;
        font-size: 17px;
        transition:0.3s;
    }
    /* Style the tab content */
    .tabcontent {
        display: none;
        background-color:rgb(0,154,200);
        padding: 6px 12px;
        color:#fff;
    }
    ul.tab li.current{
        background-color: rgb(0,154,200);
        color: #222;
    }
    .tabcontent.current {
        display: block;
    }
</style>

탭메뉴 부분에 관련된 CSS 코드입니다.

💻 jQuery

<script>
    $(function() {
        $('ul.tab li').click(function() {
            var activeTab = $(this).attr('data-tab');
            $('ul.tab li').removeClass('current');
            $('.tabcontent').removeClass('current');
            $(this).addClass('current');
            $('#' + activeTab).addClass('current');
        })
    });
</script>

jQuery를 통해서 탭메뉴의 동작부분을 손쉽게 코딩했습니다.

jQuery에서 class의 컨트롤을 손쉽게 도와주는 addClass 와 removeClass 메소드를 활용해서 구현했습니다.

💻 전체 코드

 

완성된 코드입니다. 복사해서 사용하셔도 되지만, 최대한 동작 원리를 이해하시고 사용하시면 실력 키우는데에도 많은 도움이 있을 것 같습니다.

728x90
반응형
그리드형

이 포스팅은 쿠팡파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

💖 저자에게 암호화폐로 후원하기 💖

아이콘을 클릭하면 지갑 주소가자동으로 복사됩니다