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

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

分析

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

图1-1

图1-2

实现

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

v-stack指令的实现

创建 stack 文件

js 复制代码
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 文件,方便后续统一注册

js 复制代码
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文件中统一注册

js 复制代码
import { createApp } from 'vue'
import directives from '@/directives/index'
import App from '@/App.vue'
const app = createApp(App)

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

在组件中使用自定义指令

js 复制代码
<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

js 复制代码
<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 组件代码

js 复制代码
<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>
相关推荐
人工智能训练师1 天前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
Seveny071 天前
pnpm相对于npm,yarn的优势
前端·npm·node.js
yddddddy1 天前
css的基本知识
前端·css
昔人'1 天前
css `lh`单位
前端·css
Nan_Shu_6141 天前
Web前端面试题(2)
前端
知识分享小能手1 天前
React学习教程,从入门到精通,React 组件核心语法知识点详解(类组件体系)(19)
前端·javascript·vue.js·学习·react.js·react·anti-design-vue
2501_918126911 天前
用html5写一个flappybird游戏
css·游戏·html5
蚂蚁RichLab前端团队1 天前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
孩子 你要相信光1 天前
css之一个元素可以同时应用多个动画效果
前端·css
萌萌哒草头将军1 天前
Oxc 和 Rolldown Q4 更新计划速览!🚀🚀🚀
javascript·vue.js·vite