https://medium.com/@Taha_Shashtari/hide-navbar-on-scroll-down-in-vue-fb85acbdddfe

 

Hide Navbar on Scroll Down in Vue

Content focused sites usually improve the user experience by reducing the distraction when it’s possible. And one way to achieve this is…

medium.com

 

1. 

<template>
  <div class="app">
    <div
      class="navbar"
      :class="{ 'navbar--hidden': !showNavbar }"
    ></div>
  </div>
</template>

Navbar는 간단하게 showNavbar가 false일 때, 사라질 것이다. 아직 관련 기능을 구현하지 않았다. 

 

 

2.

<style>
* {
  box-sizing: border-box;
}
body {
  padding: 0;
  margin: 0;
}
.app {
  width: 100vw;
  height: 500vh;
  background: hsl(200, 50%, 90%);
}
.navbar {
  height: 60px;
  width: 100vw;
  background: hsl(200, 50%, 50%);
  position: fixed;
  box-shadow: 0 2px 15px rgba(71, 120, 120, 0.5);
  transform: translate3d(0, 0, 0);
  transition: 0.1s all ease-out;
}
.navbar.navbar--hidden {
  box-shadow: none;
  transform: translate3d(0, -100%, 0);
}
</style>

translate3d() :

translate3d() 메소드는 현재 위치에서 해당 요소를 주어진 x축과 y축, z축의 거리만큼 이동시킨다.

주어진 거리가 양수이면 해당 축의 양의 방향으로, 음수이면 해당 축의 음의 방향으로 이동시킨다.

http://tcpschool.com/css/css3_transform_3Dtransform

 

 

3. 

<script>
export default {
  data () {
    return {
      showNavbar: true,
      lastScrollPosition: 0
    }
  }
}
</script>

 

 

4. 

mounted () {
  window.addEventListener('scroll', this.onScroll)
},
beforeDestroy () {
  window.removeEventListener('scroll', this.onScroll)
}

 

 

5. 

methods: {
  onScroll () {
    const currentScrollPosition = window.pageYOffset || document.documentElement.scrollTop
    if (currentScrollPosition < 0) {
      return
    }
    // Stop executing this function if the difference between
    // current scroll position and last scroll position is less than some offset
    if (Math.abs(currentScrollPosition - this.lastScrollPosition) < 60) {
      return
    }
    this.showNavbar = currentScrollPosition < this.lastScrollPosition
    this.lastScrollPosition = currentScrollPosition
  }
}

 

'Front-End > Vue & Nuxt' 카테고리의 다른 글

Vercel 배포  (0) 2022.02.10
Nuxt-2 ezst nuxt.config.js  (0) 2022.01.30
Nuxt.js Async, Fetch  (0) 2022.01.05
Nuxt.js 동작 정리  (0) 2021.12.28
Nuxt-1  (0) 2021.12.26

+ Recent posts