MenuScroll.vue 2.7 KB

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