实现导航栏吸顶操作

一、使用VueUse插件

javascript 复制代码
// 安装
npm i @vueuse/core

二、点击搜索useScroll

2.1搜索结果如图

三、使用

javascript 复制代码
// 这是示例代码
import { useScroll } from '@vueuse/core'
const el = ref<HTMLElement | null>(null)
const { x, y, isScrolling, arrivedState, directions } = useScroll(el)
// 我自己的代码
import { useScroll } from '@vueuse/core';
// 直接解构赋值拿到 当鼠标向下滑动时滑动的距离
const { y } = useScroll(window);
// html结构代码
<div class="box" :class="{flotbox: y > 100}"> 
    <div class="box_l">
      <img v-if="y > 100" src="@/assets/indexLogo1.png" alt="">
      <img v-else src="@/assets/indexLogo.png" alt="">
    </div>
    <div class="box_c">
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">首页</el-menu-item>
        <el-menu-item index="2">产品技术</el-menu-item>
        <el-menu-item index="3">解决方案</el-menu-item>
        <el-menu-item index="4">行业洞见</el-menu-item>
        <el-menu-item index="5">关于我们</el-menu-item>
      </el-menu>
    </div>
// css核心代码 使用的是固定定位
.box {
  position: fixed;
  top: 0;
  left: 0;
}

四、当鼠标向下滑动超过100像素时导航栏切换样式

相关推荐
excel13 分钟前
使用函数式封装绘制科赫雪花(Koch Snowflake)
前端
萌萌哒草头将军1 小时前
Node.js v24.6.0 新功能速览 🚀🚀🚀
前端·javascript·node.js
持久的棒棒君2 小时前
启动electron桌面项目控制台输出中文时乱码解决
前端·javascript·electron
小离a_a3 小时前
使用原生css实现word目录样式,标题后面的...动态长度并始终在标题后方(生成点线)
前端·css
郭优秀的笔记4 小时前
抽奖程序web程序
前端·css·css3
尚学教辅学习资料4 小时前
Vue3从入门到精通: 4.5 数据持久化与同步策略深度解析
vue·数据持久化
布兰妮甜4 小时前
CSS Houdini 与 React 19 调度器:打造极致流畅的网页体验
前端·css·react.js·houdini
小小愿望4 小时前
ECharts 实战技巧:揭秘 X 轴末项标签 “莫名加粗” 之谜及破解之道
前端·echarts
小小愿望5 小时前
移动端浏览器中设置 100vh 却出现滚动条?
前端·javascript·css
fail_to_code5 小时前
请不要再只会回答宏任务和微任务了
前端