溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vue電商網(wǎng)站首頁內(nèi)容吸頂功能怎么實(shí)現(xiàn)

發(fā)布時間:2023-04-21 16:13:24 來源:億速云 閱讀:106 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Vue電商網(wǎng)站首頁內(nèi)容吸頂功能怎么實(shí)現(xiàn)”,在日常操作中,相信很多人在Vue電商網(wǎng)站首頁內(nèi)容吸頂功能怎么實(shí)現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Vue電商網(wǎng)站首頁內(nèi)容吸頂功能怎么實(shí)現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

交互要求

  • 滾動距離大于等于78個px的時候,組件會在頂部固定定位

  • 滾動距離小于78個px的時候,組件消失隱藏

實(shí)現(xiàn)思路

  • 準(zhǔn)備一個吸頂組件,準(zhǔn)備一個類名,控制顯示隱藏

  • 監(jiān)聽頁面滾動,判斷滾動距離,距離大于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注冊scroll事件,獲取滾動距離

<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ù)膆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)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI