index.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <!-- @format -->
  2. <script setup lang="ts">
  3. import { useCommonStore } from '@/stores/modules/common'
  4. const commonStore = useCommonStore()
  5. onMounted(() => {
  6. let lastScrollY = window.scrollY
  7. const sections = document.querySelectorAll('[data-section]')
  8. const observerOptions = {
  9. root: null, // 相对于视口观察
  10. threshold: [0.1, 0.9], // 区域可见时触发
  11. }
  12. const intersectionCallback = (entries: any) => {
  13. const currentScrollY = window.scrollY
  14. const isScrollingDown = currentScrollY > lastScrollY
  15. console.log('intersectionRatio-----111', currentScrollY, lastScrollY, isScrollingDown)
  16. lastScrollY = currentScrollY
  17. entries.forEach((entry: any) => {
  18. const intersectionRatio = entry.intersectionRatio
  19. // if (isScrollingDown && intersectionRatio >= 0.1) {
  20. // console.log('intersectionRatio-----down')
  21. // // 处理向下滚动时的逻辑
  22. // const newColor = entry.target.dataset.sectionColor
  23. // commonStore.setNavigateBgColor(newColor)
  24. // }
  25. // if (!isScrollingDown && intersectionRatio >= 0.1) {
  26. // console.log('intersectionRatio-----up', entry.target)
  27. // // 处理向上滚动时的逻辑
  28. // const newColor = entry.target.dataset.sectionColor
  29. // commonStore.setNavigateBgColor(newColor)
  30. // }
  31. })
  32. }
  33. // 4. 创建观察器实例
  34. const observer = new IntersectionObserver(intersectionCallback, observerOptions)
  35. // 5. 开始观察所有区块
  36. sections.forEach(section => observer.observe(section))
  37. // 设置首屏默认颜色
  38. // if (sections.length > 0) {
  39. // const cl = sections[0].dataset.sectionColor
  40. // console.log('sections', sections, cl)
  41. // commonStore.setNavigateBgColor(cl)
  42. // }
  43. // 组件卸载时清理
  44. onUnmounted(() => {
  45. observer.disconnect()
  46. })
  47. })
  48. </script>
  49. <template>
  50. <div>
  51. <business-home-banner />
  52. <business-home-solutions />
  53. <common-block-catalogs />
  54. <common-block-partner />
  55. <common-block-exhibited />
  56. <common-block-blog />
  57. <common-block-faq />
  58. <AppFooter />
  59. </div>
  60. </template>
  61. <style lang="less" scoped></style>