MenuScroll.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <!--
  2. Menu de navigation entre les sections d'une page, accrochée au haut de l'écran sur les écrans larges
  3. -->
  4. <template>
  5. <LayoutContainer v-scroll="handleScroll">
  6. <v-row>
  7. <v-col cols="12">
  8. <v-list
  9. class="menu-container"
  10. density="compact"
  11. :class="{ 'sticky-menu': isSticky }"
  12. >
  13. <nuxt-link
  14. v-if="isSticky"
  15. :to="{ path: '', hash: '#top' }"
  16. class="px-3 py-1"
  17. >
  18. <v-icon icon="fa fa-angle-up" />
  19. </nuxt-link>
  20. <div v-for="menu in menus" :key="menu.anchor">
  21. <nuxt-link :to="{ path: '', hash: '#' + menu.anchor }">
  22. <v-list-item
  23. :class="{ active: isSticky && menu.anchor === activeMenuItem }"
  24. >
  25. {{ menu.label }}
  26. </v-list-item>
  27. </nuxt-link>
  28. </div>
  29. </v-list>
  30. </v-col>
  31. </v-row>
  32. </LayoutContainer>
  33. </template>
  34. <script setup lang="ts">
  35. import type { ComputedRef, PropType, Ref } from "vue";
  36. import type { MenuScroll } from "~/types/interface";
  37. import { useLayoutStore } from "~/stores/layoutStore";
  38. defineProps({
  39. menus: {
  40. type: Array as PropType<Array<MenuScroll>>,
  41. required: true,
  42. },
  43. });
  44. const layoutStore = useLayoutStore();
  45. const isSticky: Ref<boolean> = ref(false);
  46. const activeMenuItem: ComputedRef<string | null> = computed(() => {
  47. return (
  48. Object.entries(layoutStore.isAnchoredSectionOnScreen).find(
  49. ([_, value]) => value,
  50. )?.[0] ?? null
  51. );
  52. });
  53. const handleScroll = () => {
  54. isSticky.value = window.scrollY > 800;
  55. };
  56. </script>
  57. <style scoped lang="scss">
  58. .sticky-menu {
  59. position: fixed;
  60. top: 0 !important;
  61. left: 0;
  62. right: 0;
  63. background: var(--neutral-color);
  64. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  65. }
  66. .menu-container {
  67. top: -32px;
  68. z-index: 3;
  69. display: flex;
  70. justify-content: center;
  71. background: var(--neutral-color);
  72. color: var(--on-neutral-color);
  73. font-size: 15px;
  74. line-height: 19px;
  75. align-items: center;
  76. text-align: center;
  77. letter-spacing: 0.18em;
  78. text-transform: uppercase;
  79. border-bottom: 0.1rem solid var(--on-neutral-color-extra-light);
  80. a {
  81. text-decoration: none;
  82. color: var(--on-neutral-color);
  83. }
  84. a:hover {
  85. text-decoration: underline;
  86. }
  87. }
  88. .active {
  89. background-color: var(--scroll-menu-primary-color);
  90. color: var(--scroll-menu-on-primary-color);
  91. border-radius: 16px;
  92. padding: 5px;
  93. }
  94. </style>