实现导航栏吸顶操作

一、使用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像素时导航栏切换样式

相关推荐
王码码20357 分钟前
Flutter for OpenHarmony:Flutter 三方库 bluez 玩转 Linux 风格的蓝牙操作(蓝牙底层互操作)
linux·运维·服务器·前端·flutter·云原生·harmonyos
chilavert3182 小时前
技术演进中的开发沉思-371:final 关键字(中)
java·前端·数据库
2301_816997882 小时前
Vite构建工具
前端
yuki_uix3 小时前
深入理解 reduce:从面试题到设计思维
前端
凌云拓界3 小时前
TypeWell全攻略(二):热力图渲染引擎,让键盘发光
前端·后端·python·计算机外设·交互·pyqt·数据可视化
coding随想3 小时前
TypeScript 高级类型全攻略:从“可表达性”到“类型体操”的实践之路
前端·javascript·typescript
大时光4 小时前
gsap -滚动插件 ScrollTrigger 简单demo
前端
tangbin5830854 小时前
iOS Swift:蓝牙 BLE 连接外设CoreBluetooth
前端