使用自定义指令实现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>
相关推荐
zwjapple3 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
像风一样自由20205 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem5 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术6 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理6 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
GISer_Jing6 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止6 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall6 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴6 小时前
简单入门Python装饰器
前端·python