feat:新增导航栏吸顶效果

This commit is contained in:
Xwen
2023-12-14 17:05:39 +08:00
parent 3f40358e2d
commit 3cf0fabe15

View File

@@ -1,7 +1,11 @@
<template> <template>
<div class="common-layout"> <div class="common-layout">
<el-container> <el-container>
<el-header class="common-header"> <el-header
class="common-header"
ref="header"
:class="[isFixed ? 'fixed' : '']"
>
<AppHeader /> <AppHeader />
</el-header> </el-header>
<el-main> <el-main>
@@ -11,13 +15,35 @@
</div> </div>
</template> </template>
<script setup> <script setup>
import { ref, onMounted } from "vue";
import AppHeader from "./AppHeader.vue"; import AppHeader from "./AppHeader.vue";
import AppBody from "./AppBody.vue"; import AppBody from "./AppBody.vue";
const header = ref(null);
const isFixed = ref(false);
onMounted(() => {
window.addEventListener("scroll", handleScroll);
});
const handleScroll = () => {
const scrollTop =
window.scrollY ||
document.documentElement.scrollTop ||
document.body.scrollTop;
const currentEle = header.value.$el;
if (scrollTop > currentEle.offsetTop) {
isFixed.value = true;
} else {
isFixed.value = false;
}
};
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.common { .common {
&-header { &-header {
background-color: #fff;
width: 100%; width: 100%;
display: flex; display: flex;
justify-content: center; justify-content: center;
@@ -30,4 +56,12 @@ import AppBody from "./AppBody.vue";
min-height: 10rem; min-height: 10rem;
background-color: #f0f2f5; background-color: #f0f2f5;
} }
.fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 99;
}
</style> </style>