본문 바로가기

TUTORIAL


패럴랙스 기본 배우기

06. 패럴럭스 배우기 - Hidden Menu

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width">

    <title>Parallax</title>
    <link href="https://fonts.googleapis.com/css?family=Lato:100,100i,300,300i,400,400i,700,700i,900,900i'" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Abel" rel="stylesheet">
    <style>
        /* reset */
        * {margin:0; padding:0; font-family: 'Lato';}
        li {list-style: none;}
        a {text-decoration: none;}
        
        /* nav */
        #nav {position: absolute; left: 0; top: -61px; width: 100%; height: 61px; z-index: 1000; background-color: rgba(255,255,255,0.3); transition: all 0.3s ease;}
        #nav.on {position: fixed; top: 0;}
        #nav h1 {float: left; color: #fff; font-size: 40px; padding: 5px 5px 5px 15px; font-family: 'Abel' }
        #nav .menu {float: right; padding-right: 10px; }
        #nav .menu li {display: inline; }
        #nav .menu li a {display: inline-block; padding: 20px 15px; color: #fff; font-weight: bold; font-family: 'Abel'; text-transform: capitalize;}
        #nav .menu li.active a {color: #111;}
        
        /* contents */
        #contents {text-align: center; color: #fff;}
        #contents > div {height: 100vh; line-height: 100vh;}
        #contents > div h2 {font-size: 90px; font-weight: 200; text-transform: uppercase;}
        #contents > div h2 strong {font-weight: 700; font-style: italic;}
        
        /* section */
        #section5 {background:radial-gradient(ellipse farthest-corner at center bottom, #6beace, #2e9aa4);}
        #section2 {background:radial-gradient(ellipse farthest-corner at center top, #f39264 0%, #f2606f 100%); }
        #section3 {background:radial-gradient(ellipse farthest-corner at center top, #ffee75, #ff8930);}
        #section4 {background:radial-gradient(ellipse farthest-corner at center top, #6CEBCF 0%, #38a7aa 100%);}
        #section6 {background:radial-gradient(ellipse farthest-corner at center bottom, #69d2fb 0%, #20438E 100%);}
        #section1 {background:radial-gradient(ellipse farthest-corner at center top, #cbf49a, #1ea623);} 
    </style>
</head>
<body>
    <nav id="nav">
        <h1>WEB'S</h1>
        <ul class="menu">
            <li class="active"><a href="#section1">menu1</a></li>
            <li><a href="#section2">menu2</a></li>
            <li><a href="#section3">menu3</a></li>
            <li><a href="#section4">menu4</a></li>
            <li><a href="#section5">menu5</a></li>
            <li><a href="#section6">menu6</a></li>
        </ul>   
    </nav>

    <div id="contents">
        <div id="section1"><h2>모든 인생은 <strong>실험이다.</strong></h2></div>
        <div id="section2"><h2>시간은 <strong>금이다.</strong></h2></div>
        <div id="section3"><h2>피할 수 없으면 <strong>즐겨라!</strong></h2></div>
        <div id="section4"><h2>삶이 있는 한 <strong>희망</strong>은 있다.</h2></div>
        <div id="section5"><h2>먼저핀 꽃은 <strong>먼저진다.</strong></h2></div>
        <div id="section6"><h2>내일은 내일의 <strong>태양이 뜬다.</strong></h2></div>
    </div>
    
    <!-- script -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script>
        var nav = $("#nav ul li");
        var cont = $("#contents > div");

        nav.click(function(e){
            e.preventDefault();
            var target = $(this);
            var index = target.index();
            //alert(index);
            var section = cont.eq(index);
            var offset = section.offset().top;
            $("html,body").animate({ scrollTop:offset },600,"easeInBounce");
        });

        $(window).scroll(function(){
            var wScroll = $(this).scrollTop();
            
            if(wScroll > 200){
                $("#nav").addClass("on");
            } else if (wScroll == 0) {
                $("#nav").removeClass("on");
            }
            
            if(wScroll >= cont.eq(0).offset().top){
                nav.removeClass("active");
                nav.eq(0).addClass("active");
            }
            if(wScroll >= cont.eq(1).offset().top){
                nav.removeClass("active");
                nav.eq(1).addClass("active");
            }
            if(wScroll >= cont.eq(2).offset().top){
                nav.removeClass("active");
                nav.eq(2).addClass("active");
            }
            if(wScroll >= cont.eq(3).offset().top){
                nav.removeClass("active");
                nav.eq(3).addClass("active");
            }
            if(wScroll >= cont.eq(4).offset().top){
                nav.removeClass("active");
                nav.eq(4).addClass("active");
            }
            if(wScroll >= cont.eq(5).offset().top){
                nav.removeClass("active");
                nav.eq(5).addClass("active");
            }
        });
    </script>
</body>
</html>




<nav id="nav">
    <h1>WEB'S</h1>
    <ul class="menu">
        <li class="active"><a href="#section1">menu1</a></li>
        <li><a href="#section2">menu2</a></li>
        <li><a href="#section3">menu3</a></li>
        <li><a href="#section4">menu4</a></li>
        <li><a href="#section5">menu5</a></li>
        <li><a href="#section6">menu6</a></li>
    </ul>   
</nav>

<div id="contents">
    <div id="section1"><h2>모든 인생은 <strong>실험이다.</strong></h2></div>
    <div id="section2"><h2>시간은 <strong>금이다.</strong></h2></div>
    <div id="section3"><h2>피할 수 없으면 <strong>즐겨라!</strong></h2></div>
    <div id="section4"><h2>삶이 있는 한 <strong>희망</strong>은 있다.</h2></div>
    <div id="section5"><h2>먼저핀 꽃은 <strong>먼저진다.</strong></h2></div>
    <div id="section6"><h2>내일은 내일의 <strong>태양이 뜬다.</strong></h2></div>
</div>




<style>
    /* reset */
    * {margin:0; padding:0; font-family: 'Lato';}
    li {list-style: none;}
    a {text-decoration: none;}

    /* nav */
    #nav {position: absolute; left: 0; top: -61px; width: 100%; height: 61px; z-index: 1000; background-color: rgba(255,255,255,0.3); transition: all 0.3s ease;}
    #nav.on {position: fixed; top: 0;}
    #nav h1 {float: left; color: #fff; font-size: 40px; padding: 5px 5px 5px 15px; font-family: 'Abel' }
    #nav .menu {float: right; padding-right: 10px; }
    #nav .menu li {display: inline; }
    #nav .menu li a {display: inline-block; padding: 20px 15px; color: #fff; font-weight: bold; font-family: 'Abel'; text-transform: capitalize;}
    #nav .menu li.active a {color: #111;}

    /* contents */
    #contents {text-align: center; color: #fff;}
    #contents > div {height: 100vh; line-height: 100vh;}
    #contents > div h2 {font-size: 90px; font-weight: 200; text-transform: uppercase;}
    #contents > div h2 strong {font-weight: 700; font-style: italic;}

    /* section */
    #section5 {background:radial-gradient(ellipse farthest-corner at center bottom, #6beace, #2e9aa4);}
    #section2 {background:radial-gradient(ellipse farthest-corner at center top, #f39264 0%, #f2606f 100%); }
    #section3 {background:radial-gradient(ellipse farthest-corner at center top, #ffee75, #ff8930);}
    #section4 {background:radial-gradient(ellipse farthest-corner at center top, #6CEBCF 0%, #38a7aa 100%);}
    #section6 {background:radial-gradient(ellipse farthest-corner at center bottom, #69d2fb 0%, #20438E 100%);}
    #section1 {background:radial-gradient(ellipse farthest-corner at center top, #cbf49a, #1ea623);} 
</style>




<script>
    var nav = $("#nav ul li");
    var cont = $("#contents > div");

    nav.click(function(e){
        e.preventDefault();
        var target = $(this);
        var index = target.index();
        //alert(index);
        var section = cont.eq(index);
        var offset = section.offset().top;
        $("html,body").animate({ scrollTop:offset },600,"easeInBounce");
    });

    $(window).scroll(function(){
        var wScroll = $(this).scrollTop();

        if(wScroll > 200){
            $("#nav").addClass("on");
        } else if (wScroll == 0) {
            $("#nav").removeClass("on");
        }

        if(wScroll >= cont.eq(0).offset().top){
            nav.removeClass("active");
            nav.eq(0).addClass("active");
        }
        if(wScroll >= cont.eq(1).offset().top){
            nav.removeClass("active");
            nav.eq(1).addClass("active");
        }
        if(wScroll >= cont.eq(2).offset().top){
            nav.removeClass("active");
            nav.eq(2).addClass("active");
        }
        if(wScroll >= cont.eq(3).offset().top){
            nav.removeClass("active");
            nav.eq(3).addClass("active");
        }
        if(wScroll >= cont.eq(4).offset().top){
            nav.removeClass("active");
            nav.eq(4).addClass("active");
        }
        if(wScroll >= cont.eq(5).offset().top){
            nav.removeClass("active");
            nav.eq(5).addClass("active");
        }
    });
</script>




06. 패럴럭스 배우기 - Hidden Menu

평소에는 메뉴가 없다가 스크롤을 내리면 나타나는 튜토리얼입니다.

완성
광고
  • KKS 2019.03.06 00:42 댓글주소 수정/삭제 댓글쓰기

    http://practice001.dothome.co.kr/parallax/parallax06.html
    잘 따라했습니다~!

    질문 하나 있는데요! hidden menu 스크립트 짜다가

    http://www.oktomato.net/ 이 사이트처럼 메뉴를 표현하려고 할 때,

    http://practice001.dothome.co.kr/parallax/parallax6-1.html << 이렇게 소스를 짜도 괜찮은건가요? 궁금합니다.