Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實(shí)現(xiàn)過程
交互要求
- 滾動(dòng)距離大于等于78個(gè)px的時(shí)候,組件會(huì)在頂部固定定位
- 滾動(dòng)距離小于78個(gè)px的時(shí)候,組件消失隱藏
實(shí)現(xiàn)思路
- 準(zhǔn)備一個(gè)吸頂組件,準(zhǔn)備一個(gè)類名,控制顯示隱藏
- 監(jiān)聽頁面滾動(dòng),判斷滾動(dòng)距離,距離大于78px添加類名
核心代碼
(1)新建吸頂導(dǎo)航組件src/Layout/components/app-header-sticky.vue
<script lang="ts" setup name="AppHeaderSticky">
import AppHeaderNav from './app-header-nav.vue'
</script>
<template>
<div class="app-header-sticky">
<div class="container">
<RouterLink class="logo" to="/" />
<AppHeaderNav />
<div class="right">
<RouterLink to="/">品牌</RouterLink>
<RouterLink to="/">專題</RouterLink>
</div>
</div>
</div>
</template>
<style scoped lang="less">
.app-header-sticky {
width: 100%;
height: 80px;
position: fixed;
left: 0;
top: 0;
z-index: 999;
background-color: #fff;
border-bottom: 1px solid #e4e4e4;
.container {
display: flex;
align-items: center;
}
.logo {
width: 200px;
height: 80px;
background: url(@/assets/images/logo.png) no-repeat right 2px;
background-size: 160px auto;
}
.right {
width: 220px;
display: flex;
text-align: center;
padding-left: 40px;
border-left: 2px solid @xtxColor;
a {
width: 38px;
margin-right: 40px;
font-size: 16px;
line-height: 1;
&:hover {
color: @xtxColor;
}
}
}
}
</style>(2)Layout首頁引入吸頂導(dǎo)航組件
<script lang="ts" setup>
import AppTopnav from './components/app-topnav.vue'
import AppHeader from './components/app-header.vue'
import AppFooter from './components/app-footer.vue'
+import AppHeaderSticky from './components/app-header-sticky.vue'
</script>
<template>
<AppTopnav></AppTopnav>
<AppHeader></AppHeader>
+ <AppHeaderSticky></AppHeaderSticky>
<div class="app-body">
<!-- 路由出口 -->
<RouterView></RouterView>
</div>
<AppFooter></AppFooter>
</template>
<style lang="less" scoped>
.app-body {
min-height: 600px;
}
</style>
(3)提供樣式,控制sticky的顯示和隱藏
.app-header-sticky {
width: 100%;
height: 80px;
position: fixed;
left: 0;
top: 0;
z-index: 999;
background-color: #fff;
border-bottom: 1px solid #e4e4e4;
+ transform: translateY(-100%);
+ &.show {
+ transition: all 0.3s linear;
+ transform: translateY(0%);
+ }
(4)給window注冊(cè)scroll事件,獲取滾動(dòng)距離
<script lang="ts" setup>
import { onBeforeUnmount, onMounted, ref } from 'vue'
import AppHeaderNav from './app-header-nav.vue'
const y = ref(0)
const onScroll = () => {
y.value = document.documentElement.scrollTop
}
onMounted(() => {
window.addEventListener('scroll', onScroll)
})
onBeforeUnmount(() => {
window.removeEventListener('scroll', onScroll)
})
</script>
(5)控制sticky的顯示和隱藏
<div class="app-header-sticky" :class="{show:y >= 78}">(6)修復(fù)bug,為了吸頂頭部的內(nèi)容不遮住不吸頂?shù)念^部。

<div class="container" v-show="y >= 78">
也可以使用185px,正好原有的header全部消失時(shí)候展示吸頂?shù)膆eader
頭部分類導(dǎo)航-吸頂重構(gòu)
vueuse/core : 組合式API常用復(fù)用邏輯的集合
目標(biāo): 使用 vueuse/core 重構(gòu)吸頂功能
核心步驟
(1)安裝@vueuse/core 包,它封裝了常見的一些交互邏輯
yarn add @vueuse/core
(2)在吸頂導(dǎo)航中使用
src/components/app-header-sticky.vue
<script lang="ts" setup>
import AppHeaderNav from './app-header-nav.vue'
// import { onBeforeUnmount, onMounted, ref } from 'vue'
import { useWindowScroll } from '@vueuse/core'
// const y = ref(0)
// const onScroll = () => {
// y.value = document.documentElement.scrollTop
// }
// onMounted(() => {
// window.addEventListener('scroll', onScroll)
// })
// onBeforeUnmount(() => {
// window.removeEventListener('scroll', onScroll)
// })
// 控制是否顯示吸頂組件
const { y } = useWindowScroll()
</script>
到此這篇關(guān)于Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實(shí)現(xiàn)過程的文章就介紹到這了,更多相關(guān)Vue網(wǎng)站吸頂內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Ant Design Vue Pro動(dòng)態(tài)路由加載,服務(wù)器重啟首頁白屏問題
這篇文章主要介紹了Ant Design Vue Pro動(dòng)態(tài)路由加載,服務(wù)器重啟首頁白屏問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
解決打包后出現(xiàn)錯(cuò)誤y.a.addRoute is not a function的
這篇文章主要介紹了解決打包后出現(xiàn)y.a.addRoute is not a function的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
vue+element 多個(gè)相同的select不允許重復(fù)選擇問題
這篇文章主要介紹了vue+element 多個(gè)相同的select不允許重復(fù)選擇問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-07-07
Element?Plus的el-tree-select組件懶加載+數(shù)據(jù)回顯詳解
el-tree-select組件是el-tree和el-select的結(jié)合體,他們的原始屬性未被更改,下面這篇文章主要給大家介紹了關(guān)于Element?Plus的el-tree-select組件懶加載+數(shù)據(jù)回顯的相關(guān)資料,需要的朋友可以參考下2022-11-11
基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件
這篇文章主要為大家介紹了基于ant-design-vue實(shí)現(xiàn)表格操作按鈕組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

