MenuScroll.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. Avantages: null,
  33. Fonctionnalites: null,
  34. Comparatif: null,
  35. contact: null,
  36. Temoignages: null,
  37. formations: null,
  38. Aide: null
  39. });
  40. const isSticky = ref(false);
  41. const handleScroll = () => {
  42. const scrollPosition = window.scrollY;
  43. if (scrollPosition > 800) {
  44. isSticky.value = true;
  45. } else {
  46. isSticky.value = false;
  47. }
  48. // Mettez à jour le menu actif en fonction de la position du défilement
  49. for (const key of Object.keys(refs)) {
  50. const element = refs[key];
  51. if (element) {
  52. const top = element.offsetTop;
  53. const bottom = top + element.offsetHeight;
  54. if (scrollPosition >= top && scrollPosition < bottom) {
  55. activeMenu.value = key;
  56. break;
  57. }
  58. }
  59. }
  60. };
  61. onMounted(() => {
  62. Object.keys(refs).forEach(key => {
  63. refs[key] = document.getElementById(key);
  64. });
  65. window.addEventListener('scroll', handleScroll);
  66. });
  67. onMounted(() => {
  68. window.addEventListener('scroll', handleScroll);
  69. });
  70. onUnmounted(() => {
  71. window.removeEventListener('scroll', handleScroll);
  72. });
  73. const menus = [
  74. { label: "Presentation" },
  75. { label: "Avantages" },
  76. { label: "Fonctionnalites" },
  77. { label: "Comparatif" },
  78. { label: "contact" },
  79. { label: "formations" },
  80. { label: "Temoignages" },
  81. { label: "Aide" },
  82. ];
  83. const activeMenu = ref(menus[0].label);
  84. const navigate = (menu) => {
  85. activeMenu.value = menu.label;
  86. const element = document.getElementById(menu.label);
  87. if (element) {
  88. element.scrollIntoView({ behavior: "smooth" });
  89. }
  90. };
  91. </script>
  92. <style scoped>
  93. .sticky-menu {
  94. position: fixed;
  95. top: 0;
  96. left: 0;
  97. right: 0;
  98. background: white;
  99. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  100. }
  101. .menu-container {
  102. z-index: 1000;
  103. display: flex;
  104. justify-content: space-around;
  105. padding: 1rem 10rem;
  106. background: white;
  107. color: #bbb8b8;
  108. font-family: "Barlow";
  109. font-size: 12px;
  110. line-height: 16px;
  111. display: flex;
  112. align-items: center;
  113. text-align: center;
  114. letter-spacing: 0.18em;
  115. text-transform: uppercase;
  116. border-bottom: 0.1rem solid #eaeaea;
  117. }
  118. .v-chip.active-menu {
  119. background: black;
  120. color: white;
  121. }
  122. .menu-container div:hover {
  123. cursor: pointer;
  124. text-decoration: underline;
  125. }
  126. </style>