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