Vue電商網(wǎng)站首頁(yè)內(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)聽頁(yè)面滾動(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首頁(yè)引入吸頂導(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)站首頁(yè)內(nèi)容吸頂功能實(shí)現(xiàn)過程的文章就介紹到這了,更多相關(guān)Vue網(wǎng)站吸頂內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用vuex實(shí)現(xiàn)首頁(yè)導(dǎo)航切換不同路由的方法
這篇文章主要介紹了vue使用vuex實(shí)現(xiàn)首頁(yè)導(dǎo)航切換不同路由的方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Vue動(dòng)態(tài)修改網(wǎng)頁(yè)標(biāo)題的方法及遇到問題
Vue下有很多的方式去修改網(wǎng)頁(yè)標(biāo)題,這里總結(jié)下解決此問題的幾種方案:,需要的朋友可以參考下2019-06-06
Vue中監(jiān)視屬性和計(jì)算屬性區(qū)別解析
這篇文章主要介紹了Vue中監(jiān)視屬性和計(jì)算屬性區(qū)別,通過本文學(xué)習(xí)大家知道computed與watch配置項(xiàng)問題,computed能完成的功能,watch都可以完成,本文通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
vue+echarts實(shí)現(xiàn)動(dòng)態(tài)折線圖的方法與注意
這篇文章主要給大家介紹了關(guān)于vue+echarts實(shí)現(xiàn)動(dòng)態(tài)折線圖的方法與注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
vue實(shí)現(xiàn)模態(tài)框的通用寫法推薦
下面小編就為大家分享一篇vue實(shí)現(xiàn)模態(tài)框的通用寫法推薦,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-02-02

