Vue电商网站首页内容吸顶功能怎么实现

其他教程   发布日期:2025年03月31日   浏览次数:222

这篇文章主要介绍“Vue电商网站首页内容吸顶功能怎么实现”,在日常操作中,相信很多人在Vue电商网站首页内容吸顶功能怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Vue电商网站首页内容吸顶功能怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

交互要求

  • 滚动距离大于等于78个px的时候,组件会在顶部固定定位

  • 滚动距离小于78个px的时候,组件消失隐藏

实现思路

  • 准备一个吸顶组件,准备一个类名,控制显示隐藏

  • 监听页面滚动,判断滚动距离,距离大于78px添加类名

核心代码

(1)新建吸顶导航组件

  1. src/Layout/components/app-header-sticky.vue
  1. <script lang="ts" setup name="AppHeaderSticky">
  2. import AppHeaderNav from './app-header-nav.vue'
  3. </script>
  4. <template>
  5. <div class="app-header-sticky">
  6. <div class="container">
  7. <RouterLink class="logo" to="/" />
  8. <AppHeaderNav />
  9. <div class="right">
  10. <RouterLink to="/">品牌</RouterLink>
  11. <RouterLink to="/">专题</RouterLink>
  12. </div>
  13. </div>
  14. </div>
  15. </template>
  16. <style scoped lang="less">
  17. .app-header-sticky {
  18. width: 100%;
  19. height: 80px;
  20. position: fixed;
  21. left: 0;
  22. top: 0;
  23. z-index: 999;
  24. background-color: #fff;
  25. border-bottom: 1px solid #e4e4e4;
  26. .container {
  27. display: flex;
  28. align-items: center;
  29. }
  30. .logo {
  31. width: 200px;
  32. height: 80px;
  33. background: url(@/assets/images/logo.png) no-repeat right 2px;
  34. background-size: 160px auto;
  35. }
  36. .right {
  37. width: 220px;
  38. display: flex;
  39. text-align: center;
  40. padding-left: 40px;
  41. border-left: 2px solid @xtxColor;
  42. a {
  43. width: 38px;
  44. margin-right: 40px;
  45. font-size: 16px;
  46. line-height: 1;
  47. &:hover {
  48. color: @xtxColor;
  49. }
  50. }
  51. }
  52. }
  53. </style>

(2)Layout首页引入吸顶导航组件

  1. <script lang="ts" setup>
  2. import AppTopnav from './components/app-topnav.vue'
  3. import AppHeader from './components/app-header.vue'
  4. import AppFooter from './components/app-footer.vue'
  5. +import AppHeaderSticky from './components/app-header-sticky.vue'
  6. </script>
  7. <template>
  8. <AppTopnav></AppTopnav>
  9. <AppHeader></AppHeader>
  10. + <AppHeaderSticky></AppHeaderSticky>
  11. <div class="app-body">
  12. <!-- 路由出口 -->
  13. <RouterView></RouterView>
  14. </div>
  15. <AppFooter></AppFooter>
  16. </template>
  17. <style lang="less" scoped>
  18. .app-body {
  19. min-height: 600px;
  20. }
  21. </style>

(3)提供样式,控制sticky的显示和隐藏

  1. .app-header-sticky {
  2. width: 100%;
  3. height: 80px;
  4. position: fixed;
  5. left: 0;
  6. top: 0;
  7. z-index: 999;
  8. background-color: #fff;
  9. border-bottom: 1px solid #e4e4e4;
  10. + transform: translateY(-100%);
  11. + &.show {
  12. + transition: all 0.3s linear;
  13. + transform: translateY(0%);
  14. + }

(4)给window注册scroll事件,获取滚动距离

  1. <script lang="ts" setup>
  2. import { onBeforeUnmount, onMounted, ref } from 'vue'
  3. import AppHeaderNav from './app-header-nav.vue'
  4. const y = ref(0)
  5. const onScroll = () => {
  6. y.value = document.documentElement.scrollTop
  7. }
  8. onMounted(() => {
  9. window.addEventListener('scroll', onScroll)
  10. })
  11. onBeforeUnmount(() => {
  12. window.removeEventListener('scroll', onScroll)
  13. })
  14. </script>

(5)控制sticky的显示和隐藏

  1. <div class="app-header-sticky" :class="{show:y >= 78}">

(6)修复bug,为了吸顶头部的内容不遮住不吸顶的头部。

  1. <div class="container" v-show="y >= 78">

也可以使用185px,正好原有的header全部消失时候展示吸顶的header

头部分类导航-吸顶重构

vueuse/core : 组合式API常用复用逻辑的集合

目标: 使用 vueuse/core 重构吸顶功能

核心步骤

(1)安装@vueuse/core 包,它封装了常见的一些交互逻辑

yarn add @vueuse/core

(2)在吸顶导航中使用

src/components/app-header-sticky.vue

  1. <script lang="ts" setup>
  2. import AppHeaderNav from './app-header-nav.vue'
  3. // import { onBeforeUnmount, onMounted, ref } from 'vue'
  4. import { useWindowScroll } from '@vueuse/core'
  5. // const y = ref(0)
  6. // const onScroll = () => {
  7. // y.value = document.documentElement.scrollTop
  8. // }
  9. // onMounted(() => {
  10. // window.addEventListener('scroll', onScroll)
  11. // })
  12. // onBeforeUnmount(() => {
  13. // window.removeEventListener('scroll', onScroll)
  14. // })
  15. // 控制是否显示吸顶组件
  16. const { y } = useWindowScroll()
  17. </script>

以上就是Vue电商网站首页内容吸顶功能怎么实现的详细内容,更多关于Vue电商网站首页内容吸顶功能怎么实现的资料请关注九品源码其它相关文章!