MenuScroll.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.id"
  12. @click="navigate(menu)"
  13. >
  14. <v-chip
  15. v-if="activeMenu === menu.id"
  16. class="active-menu"
  17. >
  18. {{ menu.label }}
  19. </v-chip>
  20. <span v-else>{{ menu.label }}</span>
  21. </div>
  22. </v-col>
  23. </v-row>
  24. </LayoutContainer>
  25. </template>
  26. <script setup>
  27. import { ref, onMounted, watch } from 'vue';
  28. const props = defineProps({
  29. menus: Array
  30. });
  31. const isSticky = ref(false);
  32. const activeMenu = ref('');
  33. const scrollToElement = (element) => {
  34. if (element) {
  35. element.scrollIntoView({ behavior: "smooth" });
  36. }
  37. };
  38. const handleScroll = () => {
  39. const scrollPosition = window.scrollY;
  40. isSticky.value = scrollPosition > 800;
  41. for (const menu of props.menus) {
  42. const { element } = menu;
  43. if (element && scrollPosition >= element.offsetTop && scrollPosition < element.offsetTop + element.offsetHeight) {
  44. activeMenu.value = menu.id;
  45. break;
  46. }
  47. }
  48. };
  49. onMounted(() => {
  50. props.menus.forEach(menu => {
  51. menu.element = document.getElementById(menu.id);
  52. });
  53. window.addEventListener('scroll', handleScroll);
  54. });
  55. watch(() => props.menus, (newMenus) => {
  56. newMenus.forEach(menu => {
  57. menu.element = document.getElementById(menu.id);
  58. });
  59. }, { deep: true });
  60. const navigate = (menu) => {
  61. activeMenu.value = menu.id;
  62. scrollToElement(menu.element);
  63. };
  64. </script>
  65. <style scoped>
  66. .sticky-menu {
  67. position: fixed;
  68. top: 0;
  69. left: 0;
  70. right: 0;
  71. background: white;
  72. box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  73. }
  74. .menu-container {
  75. z-index: 3;
  76. display: flex;
  77. justify-content: space-around;
  78. background: white;
  79. color: #071b1f;
  80. font-family: "Barlow";
  81. font-size: 1rem;
  82. line-height: 19px;
  83. display: flex;
  84. align-items: center;
  85. text-align: center;
  86. letter-spacing: 0.18em;
  87. text-transform: uppercase;
  88. border-bottom: 0.1rem solid #eaeaea;
  89. }
  90. .v-chip.active-menu {
  91. background: var(--Vert-100, #091D20);;
  92. color: white;
  93. }
  94. .menu-container div:hover {
  95. cursor: pointer;
  96. text-decoration: underline;
  97. z-index: 15;
  98. }
  99. </style>