MenuScroll.vue 2.7 KB

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