MenuScroll.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 v-for="menu in menus" :key="menu.label" @click="navigate(menu)">
  10. <v-chip v-if="activeMenu === menu.label" class="active-menu">
  11. {{ menu.label }}
  12. </v-chip>
  13. <span v-else>{{ menu.label }}</span>
  14. </div>
  15. </v-col>
  16. </v-row>
  17. </LayoutContainer>
  18. </template>
  19. <script setup>
  20. import { ref, onMounted, onUnmounted, reactive } from "vue";
  21. const refs = reactive({
  22. about: null,
  23. valeurs: null,
  24. logiciels: null,
  25. agenda: null,
  26. histoire: null,
  27. equipe: null,
  28. });
  29. const menus = [
  30. { label: "Qui sommes-nous" },
  31. { label: "Nos valeurs" },
  32. { label: "Nos logiciels" },
  33. { label: "L'agenda opentalent" },
  34. { label: "Notre Histoire" },
  35. { label: "Notre équipe" },
  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 activeMenu = ref(menus[0].label);
  67. const navigate = (menu) => {
  68. activeMenu.value = menu.label;
  69. const element = document.getElementById(menu.label);
  70. if (element) {
  71. element.scrollIntoView({ behavior: "smooth" });
  72. }
  73. };
  74. </script>
  75. <style scoped>
  76. .sticky-menu {
  77. position: fixed;
  78. top: 0;
  79. left: 0;
  80. right: 0;
  81. background: white;
  82. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  83. }
  84. .menu-container {
  85. z-index: 3;
  86. display: flex;
  87. justify-content: space-around;
  88. background: white;
  89. color: #071b1f;
  90. font-family: "Barlow";
  91. font-size: 1rem;
  92. line-height: 19px;
  93. display: flex;
  94. align-items: center;
  95. text-align: center;
  96. letter-spacing: 0.18em;
  97. text-transform: uppercase;
  98. border-bottom: 0.1rem solid #eaeaea;
  99. }
  100. .v-chip.active-menu {
  101. background: var(--Vert-100, #091D20);;
  102. color: white;
  103. }
  104. .menu-container div:hover {
  105. cursor: pointer;
  106. text-decoration: underline;
  107. z-index: 15;
  108. }
  109. </style>