test.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <script setup lang="ts">
  2. import { onMounted, ref } from 'vue'
  3. const navRef = ref<HTMLElement | null>(null)
  4. const currentSection = ref('')
  5. function updateNavBackground(section: Element) {
  6. const backgroundColor = window.getComputedStyle(section).backgroundColor
  7. if (navRef.value)
  8. navRef.value.style.backgroundColor = backgroundColor
  9. }
  10. onMounted(() => {
  11. // Intersection Observer for when sections come into view
  12. const observer = new IntersectionObserver(
  13. (entries) => {
  14. console.log('scrollPosition---------1')
  15. entries.forEach((entry) => {
  16. if (entry.isIntersecting) {
  17. currentSection.value = entry.target.id
  18. updateNavBackground(entry.target)
  19. }
  20. })
  21. },
  22. {
  23. threshold: 0.5,
  24. rootMargin: '-80px 0px 0px 0px', // Adjust based on your nav height
  25. },
  26. )
  27. // Observe all sections
  28. document.querySelectorAll('.section').forEach((section) => {
  29. observer.observe(section)
  30. })
  31. // Scroll event handler for continuous updates
  32. // 获取id为app-scroller的元素
  33. const appScroller = document.getElementById('app-scroller')
  34. appScroller.addEventListener('scroll', () => {
  35. console.log('scrollPosition---------2', appScroller?.scrollTop)
  36. const sections = document.querySelectorAll('.section')
  37. const scrollPosition = window.scrollY + 80 // Add nav height offset
  38. sections.forEach((section) => {
  39. const sectionTop = section.offsetTop
  40. const sectionHeight = section.offsetHeight
  41. if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight)
  42. updateNavBackground(section)
  43. })
  44. })
  45. })
  46. </script>
  47. <template>
  48. <div class="app">
  49. <nav ref="navRef" class="navigation">
  50. <div class="nav-content">
  51. <h1>Logo</h1>
  52. <div class="nav-links">
  53. <a href="#section1">Section 1</a>
  54. <a href="#section2">Section 2</a>
  55. <a href="#section3">Section 3</a>
  56. <a href="#section4">Section 4</a>
  57. </div>
  58. </div>
  59. </nav>
  60. <main>
  61. <section id="section1" class="section" style="background-color: #f8f9fa;">
  62. <h2>Section 1</h2>
  63. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  64. </section>
  65. <section id="section2" class="section" style="background-color: #e9ecef;">
  66. <h2>Section 2</h2>
  67. <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  68. </section>
  69. <section id="section3" class="section" style="background-color: #dee2e6;">
  70. <h2>Section 3</h2>
  71. <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
  72. </section>
  73. <section id="section4" class="section" style="background-color: #ced4da;">
  74. <h2>Section 4</h2>
  75. <p>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
  76. </section>
  77. </main>
  78. </div>
  79. </template>
  80. <style>
  81. * {
  82. margin: 0;
  83. padding: 0;
  84. box-sizing: border-box;
  85. }
  86. body {
  87. margin: 0;
  88. padding: 0;
  89. min-height: 100vh;
  90. }
  91. .app {
  92. min-height: 100vh;
  93. width: 100%;
  94. }
  95. .navigation {
  96. position: fixed;
  97. top: 0;
  98. left: 0;
  99. right: 0;
  100. height: 80px;
  101. transition: background-color 0.3s ease;
  102. z-index: 1000;
  103. }
  104. .nav-content {
  105. max-width: 1200px;
  106. margin: 0 auto;
  107. padding: 0 20px;
  108. height: 100%;
  109. display: flex;
  110. align-items: center;
  111. justify-content: space-between;
  112. }
  113. .nav-links {
  114. display: flex;
  115. gap: 20px;
  116. }
  117. .nav-links a {
  118. color: #333;
  119. text-decoration: none;
  120. font-weight: 500;
  121. transition: color 0.3s ease;
  122. }
  123. .nav-links a:hover {
  124. color: #000;
  125. }
  126. main {
  127. padding-top: 80px;
  128. width: 100%;
  129. }
  130. .section {
  131. min-height: 100vh;
  132. width: 100%;
  133. padding: 40px;
  134. display: flex;
  135. flex-direction: column;
  136. justify-content: center;
  137. align-items: center;
  138. text-align: center;
  139. }
  140. h1 {
  141. margin: 0;
  142. font-size: 24px;
  143. }
  144. h2 {
  145. font-size: 32px;
  146. margin-bottom: 20px;
  147. }
  148. p {
  149. max-width: 600px;
  150. line-height: 1.6;
  151. margin: 0;
  152. }
  153. </style>