CSS&JS/⚡Thinkers

twitter app header처럼 vanilla JS로 스크롤에 따라 내려오고 올라가는 헤더 만들기

arancia_ 2021. 4. 9. 09:47

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>twitter App Header 구현하기</title>
<link rel="shortcut icon" href="#"/>
<link href="./twitterApp.css" rel="stylesheet" type="text/css"/>
<script src="./twitterApp_3.js" defer></script>
</head>
<body>
<!-- https://codepen.io/lehollandaisvolant/pen/ryrrGx -->
<!-- https://zellwk.com/blog/css-translate-values-in-javascript/ -->
<header>
    <nav class="cHeader" id="myNav_1">This is Header</nav>
    <nav class="cHeader" id="myNav_2">NAV</nav>
</header>
 
<section class="sect">hello world!</section>
<section class="sect">hello world!</section>
<section class="sect">hello world!</section>
<section class="sect">hello world!</section>
<section class="sect">hello world!</section>
<section class="sect">hello world!</section>
 
</body>
</html>
cs




CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@charset "utf-8";
*{margin:0;padding:0;box-sizing:border-box;}
html,body{
    overflow-x:hidden;
    width:100%;min-height:100vh;}
/*  */
header{ 
    position:fixed; z-index:50;
    top:0;
    transform:translateY(0);
    width:100%;}
 
.cHeader{
    position:relative; 
    width:100%;
    border-bottom:1px solid #000;
    box-shadow:0 5px 10px rgba(0,0,0,.5);
    text-align:center;}
#myNav_1{
    z-index:100;
    transform:translateY(0);
    height:120px;line-height:120px;
    background:#ccc;
    font-weight:bold;font-size:2rem;}
#myNav_2{
    z-index:80;
    transform:translateY(0);
    background:rgba(255,250,255.5);
    backdrop-filter:blur(5px);
    height:80px;line-height:80px;}
 
 
/*  */
.sect{
    position:relative;
    width:100%;height:100vh;
    line-height:100vh;
    background:rgb(192, 167, 157);
    border:3px solid #d2d2d2;
    text-align:center;}
.sect:nth-child(even){background:salmon;}
 
section:first-of-type{margin-top:200px;}
cs




JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
let scrollPos = 0;
let gbcrTop;
 
let myNav_1 = document.getElementById('myNav_1');
let myNav_2 = document.getElementById('myNav_2');
 
let matrix_1 = window.getComputedStyle(myNav_1).transform;
let matrix_2 = window.getComputedStyle(myNav_2).transform;
// console.log(matrix_1,matrix_2);
let nowTop_1;
 
let i= 0;
let j = 0;
 
window.addEventListener('scroll',function(){
    gbcrTop = document.body.getBoundingClientRect().top; 
    matrix_1 = window.getComputedStyle(myNav_1).transform;
    matrix_2 = window.getComputedStyle(myNav_2).transform;
 
    m1_v = matrix_1.match(/matrix.*\((.+)\)/)[1].split(', ')[5];
    m2_v = matrix_2.match(/matrix.*\((.+)\)/)[1].split(', ')[5];
 
    if(scrollPos > gbcrTop){
        //console.log('down');
        i -= 8;
        j -= 8;
        if(m1_v < -200){
            m1_v = -200;
            i = -200;
            j = -200;
        }else{
            myNav_1.style.transform = `translateY(${i}px)`;
            myNav_2.style.transform = `translateY(${j}px)`;
        }
    }else{
        //console.log('up');
        i += 10;
        if(m1_v < 0){
            myNav_1.style.transform = `translateY(${i}px)`;
        }else if(m1_v >= 0 && m2_v < 0){
            i=0;
            j+=10;
            myNav_1.style.transform = `translateY(0px)`;
            myNav_2.style.transform = `translateY(${j}px)`;
        }else{
            i = 0;
            j = 0;
        }
    }//if else
 
    scrollPos = gbcrTop;
 
    if(window.pageYOffset == 0){
        myNav_1.style.transform = `translateY(0px)`;
        myNav_2.style.transform = `translateY(0px)`;
    }
}); //scroll
cs




소스코드

twitter app header 구현(scroll event).zip
0.00MB