使用自定义指令实现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>
相关推荐
Ustinian_310几秒前
【HTML】KaTeX 常用公式字符
前端·chrome·html
OpenTiny社区14 分钟前
直播分享|TinyPro:一行命令,搭建包含前后端的后台管理系统
前端·vue.js·github
怪兽也会哭哭18 分钟前
vue-如何将组件内容作为图片生成-html2canvas
前端·javascript·vue.js
万少43 分钟前
常见鸿蒙应用开发面试题
前端
云只上1 小时前
为什么后端接口返回数字类型1.00前端会取到1?
java·前端
枫无痕1 小时前
路由权限的分类与踩坑记录
前端
冬枳1 小时前
vite+ts发布 npm 包过程记录
vue.js
best6661 小时前
ServiceWoker是什么?
前端
林中焰1 小时前
Vite + Vue3 + TS从零搭建企业级项目脚手架
vue.js