MenuScroll.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <LayoutContainer>
  3. <v-row >
  4. <v-col
  5. cols="12"
  6. class="menu-container"
  7. :class="{ 'sticky-menu': isSticky }"
  8. >
  9. <div
  10. v-for="menu in menus"
  11. :key="menu.label"
  12. @click="navigate(menu)"
  13. >
  14. <v-chip
  15. v-if="activeMenu === menu.label"
  16. class="active-menu"
  17. >
  18. {{
  19. menu.label
  20. }}
  21. </v-chip>
  22. <span v-else>{{ menu.label }}</span>
  23. </div>
  24. </v-col>
  25. </v-row>
  26. </LayoutContainer>
  27. </template>
  28. <script setup>
  29. import { ref, onMounted, onUnmounted, reactive } from 'vue';
  30. const refs = reactive({
  31. Presentation: null,
  32. Catalogue: null,
  33. Financement: null,
  34. Certification: null,
  35. Inscription: null,
  36. Evaluation: null,
  37. });
  38. const menus = [
  39. { label: 'Présentation' },
  40. { label: 'Catalogue' },
  41. { label: 'Financement' },
  42. { label: 'Certification' },
  43. { label: 'Inscription' },
  44. { label: 'Evaluation' },
  45. ];
  46. const isSticky = ref(false);
  47. const handleScroll = () => {
  48. const scrollPosition = window.scrollY;
  49. if (scrollPosition > 800) {
  50. isSticky.value = true;
  51. } else {
  52. isSticky.value = false;
  53. }
  54. for (const key of Object.keys(refs)) {
  55. const element = refs[key];
  56. if (element) {
  57. const top = element.offsetTop;
  58. const bottom = top + element.offsetHeight;
  59. if (scrollPosition >= top && scrollPosition < bottom) {
  60. activeMenu.value = key;
  61. break;
  62. }
  63. }
  64. }
  65. };
  66. onMounted(() => {
  67. Object.keys(refs).forEach(key => {
  68. refs[key] = document.getElementById(key);
  69. });
  70. window.addEventListener('scroll', handleScroll);
  71. });
  72. onMounted(() => {
  73. window.addEventListener('scroll', handleScroll);
  74. });
  75. const activeMenu = ref(menus[0].label);
  76. const navigate = (menu) => {
  77. activeMenu.value = menu.label;
  78. const element = document.getElementById(menu.label);
  79. if (element) {
  80. element.scrollIntoView({ behavior: "smooth" });
  81. }
  82. };
  83. </script>
  84. <style scoped>
  85. .sticky-menu {
  86. position: fixed;
  87. top: 0;
  88. left: 0;
  89. right: 0;
  90. background: white;
  91. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  92. }
  93. .menu-container {
  94. z-index: 3;
  95. display: flex;
  96. justify-content: space-around;
  97. background: white;
  98. color: #071b1f;
  99. font-family: "Barlow";
  100. font-size: 1rem;
  101. line-height: 19px;
  102. display: flex;
  103. align-items: center;
  104. text-align: center;
  105. letter-spacing: 0.18em;
  106. text-transform: uppercase;
  107. border-bottom: 0.1rem solid #eaeaea;
  108. }
  109. .v-chip.active-menu {
  110. background: var(--Vert-100, #091D20);;
  111. color: white;
  112. }
  113. .menu-container div:hover {
  114. cursor: pointer;
  115. text-decoration: underline;
  116. z-index: 15;
  117. }
  118. </style>