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

使用自定义指令实现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>

http://www.kler.cn/a/598308.html

相关文章:

  • 使用PyTorch Lightning进行深度学习模型训练
  • 时序数据库QuestDB在Winform窗体应用
  • XSS 攻击向量与绕过技巧
  • 银河麒麟桌面版包管理器(三)
  • conda 常用命令
  • datetime“陷阱”与救赎:扒“时间差值”证道
  • 【计算机网络】网络编程
  • Perl语言的计算机视觉
  • 可视化动态表单动态表单界的天花板--Formily(阿里开源)
  • 基于django美团美食销售数据分析与可视化系统设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
  • 测试用例设计方法与Prompt转化:一键生成高效提示词的实用指南
  • 题型笔记 | Apriori算法
  • 从零构建大语言模型全栈开发指南:第二部分:模型架构设计与实现-2.1.2多头注意力扩展与掩码机制(因果掩码与填充掩码)
  • 阿里云搭建docker私有仓库
  • [RoarCTF 2019]Easy Calc-3.23BUUCTF练习day5(2)
  • WPF控件DataGrid介绍
  • STM32HAL库,解决串口UART中断接收到的第一个字节数据丢失
  • 解密模型上下文协议(MCP):下一代AI交互框架
  • Redis为什么用跳表实现有序集合?
  • HTML 表单处理进阶:验证与提交机制的学习心得与进度(二)