Vue 设置导航吸顶

要求

在浏览器上下滚动的时候,如何距离的顶部的距离大于78px,吸顶显示,小于78px则隐藏

环境搭建

工具类安装:npm i @vueuse/core

创建吸顶页面 fixed.vue

xml 复制代码
<script setup>
// vueUse
import { useScroll } from '@vueuse/core'
const { y } = useScroll(window)

</script>

<template>
  <div class="app-header-sticky" :class="{ show: y > 78 }">
    <div class="container">
      <RouterLink class="logo" to="/" />
      <!-- 导航区域 -->
      <div class="right">
        <RouterLink to="/">品牌</RouterLink>
        <RouterLink to="/">专题</RouterLink>
      </div>
    </div>
  </div>
</template>


<style scoped lang='scss'>
.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;
  }

  .container {
    display: flex;
    align-items: center;
  }

  .logo {
    width: 200px;
    height: 80px;
    background: url("@/assets/images/logo.png") no-repeat right 2px;
    background-size: 160px auto;
  }

  .right {
    width: 220px;
    display: flex;
    text-align: center;
    padding-left: 40px;
    border-left: 2px solid $xtxColor;

    a {
      width: 38px;
      margin-right: 40px;
      font-size: 16px;
      line-height: 1;

      &:hover {
        color: $xtxColor;
      }
    }
  }
}
</style>

引用该组件:

效果演示

相关推荐
用户5806139393001 分钟前
前端文件下载实现深度解析:Blob与ObjectURL的完美协作
前端
Lin86664 分钟前
Vue 3 + TypeScript 组件类型推断失败问题完整解决方案
前端
coding随想4 分钟前
从零开始:前端开发者的SEO优化入门与实战
前端
前端工作日常7 分钟前
我理解的JSBridge
前端
Au_ust7 分钟前
前端模块化
前端
顺丰同城前端技术团队7 分钟前
还不会用 Charles?最后一遍了啊!
前端
BUG收容所所长9 分钟前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法
彬师傅9 分钟前
geojson、csv、json 数据加载
前端
用户52709648744909 分钟前
🔥 我与 ESLint 的爱恨纠葛:从"这破玩意儿"到"真香警告"
前端
梨子同志10 分钟前
手动实现 JavaScript 的 call、apply 和 bind 方法
前端·javascript