본문 바로가기

TUTORIAL


패럴랙스 기본 배우기

07. 패럴럭스 배우기 - Scroll Show/Hide 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: fixed; left: 0; top: 0px; 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: -61px;}
        #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 */
        #section1 {background:radial-gradient(ellipse farthest-corner at center bottom, #3AA17E, #00537E);}
        #section2 {background:radial-gradient(ellipse farthest-corner at center top, #A3A1FF, #3A3897); }
        #section3 {background:radial-gradient(ellipse farthest-corner at center top, #29ABE2, #4F00BC);}
        #section4 {background:radial-gradient(ellipse farthest-corner at center top, #FF5300, #45145A);}
        #section5 {background:radial-gradient(ellipse farthest-corner at center bottom, #852D91, #312A6C);}
        #section6 {background:radial-gradient(ellipse farthest-corner at center top, #FBB03B, #D4145A);}
    </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 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 >= 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");
            }
        });
        
        var wHeight = $(window).height();
        var dHeight = $(document).height();
        var navHeight = $("#nav").outerHeight();
        var lastScrollTop = 0;
        var moveScroll;

        $(window).scroll(function(event){ //스크롤을 감지
            moveScroll = true;
        });

        setInterval(function(){ //스크롤이 감지가 되면 0,25마다 실행
            if(moveScroll){
                hasScroll();
                moveScroll = false;
            }
        },250);

        function hasScroll(){
            var wScroll = $(this).scrollTop();  //현재 스크롤 값

            if( wScroll > lastScrollTop && wScroll > navHeight ){
                 //스크롤을 내렸을 때 
                 $("#nav").addClass("on");
            }else {
                 //스크롤을 올렸을 때 
                 if(wScroll + wHeight < dHeight) {
                     $("#nav").removeClass("on");
                 }
            }
            lastScrollTop = wScroll;
        }
    </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: fixed; left: 0; top: 0px; 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: -61px;}
    #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 */
    #section1 {background:radial-gradient(ellipse farthest-corner at center bottom, #3AA17E, #00537E);}
    #section2 {background:radial-gradient(ellipse farthest-corner at center top, #A3A1FF, #3A3897); }
    #section3 {background:radial-gradient(ellipse farthest-corner at center top, #29ABE2, #4F00BC);}
    #section4 {background:radial-gradient(ellipse farthest-corner at center top, #FF5300, #45145A);}
    #section5 {background:radial-gradient(ellipse farthest-corner at center bottom, #852D91, #312A6C);}
    #section6 {background:radial-gradient(ellipse farthest-corner at center top, #FBB03B, #D4145A);}
</style>




<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 >= 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");
        }
    });

    var wHeight = $(window).height();
    var dHeight = $(document).height();
    var navHeight = $("#nav").outerHeight();
    var lastScrollTop = 0;
    var moveScroll;

    $(window).scroll(function(event){ //스크롤을 감지
        moveScroll = true;
    });

    setInterval(function(){ //스크롤이 감지가 되면 0,25마다 실행
        if(moveScroll){
            hasScroll();
            moveScroll = false;
        }
    },250);

    function hasScroll(){
        var wScroll = $(this).scrollTop();  //현재 스크롤 값

        if( wScroll > lastScrollTop && wScroll > navHeight ){
             //스크롤을 내렸을 때 
             $("#nav").addClass("on");
        }else {
             //스크롤을 올렸을 때 
             if(wScroll + wHeight < dHeight) {
                 $("#nav").removeClass("on");
             }
        }
        lastScrollTop = wScroll;
    }
</script>




07. 패럴럭스 배우기 - Scroll Show/Hide Menu

스크롤을 내리면 없어지고 올리면 나타나는 메뉴 튜토리얼입니다.

완성
광고
  • oribal81 2019.01.09 11:31 댓글주소 수정/삭제 댓글쓰기

    http://oribal81.dothome.co.kr/parallax/parallax07.html
    스크롤 감지 하는 스크립트 부분을 변경해 봤는데요. 이렇게 작성해도 확장성에 문제가 없을지 궁금해서 요
    email : oribal81@naver.com