吸顶导航功能实现

  1. 顶组件核心样式:
.app-header-sticky {
  width: 100%;
  height: 80px;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;
  // 此处为关键样式!!!
  // 状态一:往上平移自身高度 + 完全透明
  transform: translateY(-100%);
  opacity: 0;

  // 状态二:移除平移 + 完全不透明
  &.show {
    transition: all 0.3s linear;
    transform: none;
    opacity: 1;
  }

  // ...
}

可以看出 show 类名控制着是否出现吸顶导航。

  1. 获取移动距离:

为了简便,直接使用 Vueuse 中的方法 useScroll。

npm i @vueuse/core
import {useScroll} from '@vueuse/core'

const {y} = useScroll(window)

吸顶导航功能实现_第1张图片

你可能感兴趣的:(vue.js,前端)