当前位置: 首页 > news >正文

使用自定义指令实现css样式层叠

使用自定义指令实现css样式层叠

分析

有时候页面的头部可能会采用固定定位的方式,同时头部占用了较大空间,导致内容区的位置被压缩,如图1-1,这时能否在滚动的时候改变内容区div的css层级,让其覆盖在头部上面(如图1-2)呢

在这里插入图片描述

图1-1

在这里插入图片描述图1-2

实现

这里采用自定义指令对滚动事件进行监听,滚动到期望位置后,修改样式实现层级覆盖。

v-stack指令的实现

创建 stack 文件

import { DirectiveBinding, VNode, Directive } from 'vue';

// binding 设置的偏移量 滚动多少像素后开始设置层级
const stack = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    let _offsetTop = el.offsetTop;
    window.addEventListener('scroll', (e: Event) => {
      const curScrollTop = (e.target as Document)?.scrollingElement?.scrollTop as number;
      if(curScrollTop > (_offsetTop - (binding.value || 0))) {
        el.style.setProperty('position', 'relative');
        el.style.setProperty('z-index', '999999');
      } else {
        el.style.removeProperty('position');
        el.style.removeProperty('z-index');
        el.style.removeProperty('top');
      };
    });
  };
};

export default stack as Directive;

将指令导入 index 文件,方便后续统一注册

import { App } from 'vue'
import { Directive } from 'vue'
import stack from './stack'

const directives = {
  stack,
} as Record<string, Directive>

export default {
  install(app: App) {
    Object.keys(directives).forEach(key => {
      app.directive(key, directives[key])
    })
  }
}

在main.ts文件中统一注册

import { createApp } from 'vue'
import directives from '@/directives/index'
import App from '@/App.vue'
const app = createApp(App)

app.use(directives)
// 省略其他...

在组件中使用自定义指令

<template>
  <div>
    <Header :style="`height: ${headerHeight}px;`"></Header>
    <div style="height: 580px; text-align: center; background-color: skyblue;">这是其他</div>
    <Container v-stack="headerHeight"></Container>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import Container from './container.vue';
import Header from './header.vue';

const headerHeight = ref(400);

</script>

<style lang="scss" scoped>
  
</style>

头部组件代码 header.vue

<template>
  <div class="header">
    <h4>这是头部</h4>
  </div>
</template>

<style lang="scss" scoped>
  .header {
    position: fixed;
    width: 100%;
    background-color: aqua;
    z-index: 200;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

container.vue 组件代码

<template>
  <div class="box">
    <ul>
      <li v-for="(item, index) in 500" :key="index"><h5>这是内容{{ item }}</h5></li>
    </ul>
  </div>
</template>

<style lang="scss" scoped>
.box {
  width: 600px;
  margin: 0 auto;
  height: 100%;
  background: pink;
  padding: 20px;
  li {
    line-height: 26px;
    text-align: center;
  }
}
</style>

相关文章:

  • DeDeCMS漏洞
  • Matrix-Breakout-2-Morpheus靶场通关心得:技巧与经验分享
  • 机器学习-手搓KNN算法
  • 小程序API —— 54 路由与通信 - 编程式导航
  • Mistral AI发布开源多模态模型Mistral Small 3.1:240亿参数实现超越GPT-4o Mini的性能
  • Handler消息机制源码分析
  • 从模拟到现实:Sensodrive高精度力反馈技术赋能物流运输的高效与安全
  • 基于Wasm的边缘计算Pandas:突破端侧AI的最后一公里——让数据分析在手机、IoT设备上飞驰
  • 若依 Excel导入与导出 配置下拉选项
  • 通过蒸馏大模型训练建筑向智能助手模型的思路
  • 63. 根文件系统构建
  • STM32 模拟SPI 模式0
  • 大模型+知识图谱:赋能知识智能新升级
  • Harmony OS【获取本地json数据的方法】
  • 【el-upload】el-upload组件 - list-type=“picture“ 时,文件预览展示优化
  • dfs(二十)257. 二叉树的所有路径
  • /proc/[pid]/maps介绍和pmap介绍、RSS
  • 《深入理解 TypeScript:函数类型与泛型全解析》(万字长文)
  • 【MyDB】5-索引管理之 1-索引管理思路概览
  • Centos7配置本地yum源
  • 特朗普:将于19日分别与普京和泽连斯基通话
  • AI赋能科学红毯,机器人与科学家在虚实之间叩问“科学精神”
  • 著名文博专家吴远明因交通事故离世,享年75岁
  • 俄媒:俄乌伊斯坦布尔谈判将于北京时间今天17时30分开始
  • 自强!助残!全国200个集体和260名个人受到表彰
  • 定制基因编辑疗法治愈罕见遗传病患儿