• 顶部动态菜单栏的使用


    效果图

     

    开发环境


    vue3

    关键逻辑

    1. //导航栏状态选择
    2. const navbarSolid = ref(false);
    3. //初始化导航栏高度
    4. const navbarHeight = ref(0);
    5. /**
    6. * 根据滚动距离改变样式
    7. */
    8. function checkNavbarOpacity() {
    9. navbarSolid.value = window.pageYOffset > navbarHeight.value / 2;
    10. }
    11. /**
    12. * 绑定window的scroll事件
    13. */
    14. onMounted(() => {
    15. // 获取导航栏高度
    16. navbarHeight.value = document.querySelector('nav').offsetHeight;
    17. window.addEventListener('scroll', checkNavbarOpacity);
    18. });
    19. /**
    20. * 解绑window的scroll事件
    21. */
    22. onUnmounted(() => {
    23. window.removeEventListener('scroll', checkNavbarOpacity);
    24. });

    实现代码

    1. <template>
    2. <nav :class="{ 'afterNav': navbarSolid, 'beforeNav': !navbarSolid }">
    3. <div class="text">
    4. <span class="nav-logo" @click="router.push('/')">月木天上</span>
    5. <div class="nav-list">
    6. <div class="nav-item" v-for="(nav,index) in navList" :key="index">
    7. <span @click="router.push(nav.path)">{{ nav.name }}</span>
    8. </div>
    9. </div>
    10. </div>
    11. </nav>
    12. </template>
    13. <script setup>
    14. import {onMounted, onUnmounted, reactive, ref} from 'vue';
    15. import {useDataStore} from "@/stores/dataStore"
    16. import {useRouter} from "vue-router";
    17. const router = useRouter()
    18. const dataStore = useDataStore()
    19. //初始化导航列表
    20. const navList = reactive([
    21. {
    22. name: '首页',
    23. path: '/home/index'
    24. },
    25. {
    26. name: '导航',
    27. path: '/home/nav'
    28. },
    29. {
    30. name: '课程',
    31. path: '/course/index'
    32. },
    33. {
    34. name: '博客',
    35. path: '/blog/index'
    36. },
    37. {
    38. name: '商城',
    39. path: '/shop/index'
    40. },
    41. {
    42. name: '联系我们',
    43. path: '/home/contact'
    44. },
    45. ])
    46. //导航栏状态选择
    47. const navbarSolid = ref(false);
    48. //初始化导航栏高度
    49. const navbarHeight = ref(0);
    50. /**
    51. * 根据滚动距离改变样式
    52. */
    53. function checkNavbarOpacity() {
    54. navbarSolid.value = window.pageYOffset > navbarHeight.value / 2;
    55. }
    56. /**
    57. * 绑定window的scroll事件
    58. */
    59. onMounted(() => {
    60. // 获取导航栏高度
    61. navbarHeight.value = document.querySelector('nav').offsetHeight;
    62. window.addEventListener('scroll', checkNavbarOpacity);
    63. });
    64. /**
    65. * 解绑window的scroll事件
    66. */
    67. onUnmounted(() => {
    68. window.removeEventListener('scroll', checkNavbarOpacity);
    69. });
    70. </script>
    71. <style scoped lang="scss">
    72. /* 初始样式 */
    73. .beforeNav {
    74. position: fixed;
    75. width: 100%;
    76. transition: background-color 0.3s;
    77. .text {
    78. margin-top: 30px;
    79. .nav-logo {
    80. color: #fff;
    81. float: left;
    82. margin-left: 12%;
    83. font-family: 楷体;
    84. font-weight: bolder;
    85. font-size: 2em;
    86. }
    87. .nav-logo:hover {
    88. cursor: pointer;
    89. }
    90. .nav-list {
    91. display: flex;
    92. margin-left: 49%;
    93. .nav-item {
    94. margin-left: 30px;
    95. font-family: 楷体;
    96. font-size: 1.5em;
    97. color: #a5d2e3;
    98. }
    99. .nav-item:hover {
    100. text-decoration: underline;
    101. cursor: pointer; /*变小手*/
    102. color: #ffffff;
    103. }
    104. }
    105. }
    106. }
    107. /* 下拉后样式 */
    108. .afterNav {
    109. background-color: #ffffff;
    110. z-index: 100;
    111. position: fixed;
    112. width: 100%;
    113. transition: background-color 0.3s;
    114. box-shadow: 0 0 9px 0 rgb(0 0 0 / 10%);
    115. .text {
    116. margin-top: 15px;
    117. margin-bottom: 15px;
    118. .nav-logo {
    119. color: #52d3aa;
    120. float: left;
    121. margin-left: 12%;
    122. font-family: 楷体;
    123. font-weight: bolder;
    124. font-size: 2em;
    125. }
    126. .nav-logo:hover {
    127. cursor: pointer;
    128. }
    129. .nav-list {
    130. display: flex;
    131. margin-left: 50%;
    132. .nav-item {
    133. margin-left: 30px;
    134. font-family: 楷体;
    135. font-size: 1.5em;
    136. color: #7f7f7f;
    137. }
    138. .nav-item:hover {
    139. text-decoration: underline;
    140. cursor: pointer; /*变小手*/
    141. color: #52d3aa;
    142. }
    143. }
    144. }
    145. }
    146. </style>

  • 相关阅读:
    用Unity同时开发【微信小游戏】【安卓】【IOS】游戏#6. 网络通信
    LeetCode每日一题(263. Ugly Number)
    excel 表格横向拼接(列拼接)
    QT链接redis
    office365 outlook邮件无法删除
    DNS抓包
    2021-01-01 - Review代码过程感悟
    农业银行半年报:一份检验韧性的优质答卷
    Python&JS宏 实现保留样式合并表格后拆分
    C++读取注册表
  • 原文地址:https://blog.csdn.net/m0_63040701/article/details/134468627