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