App.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <div class="scroll-wrapper" ref="scroll" @scroll="scrollEvent">
  3. <app-bar ref="bar"></app-bar>
  4. <router-view v-slot="{ Component }">
  5. <keep-alive :include="keeps">
  6. <component :is="Component" class="scroll-content" ref="content" :style="scrollStyle"/>
  7. </keep-alive>
  8. </router-view>
  9. </div>
  10. </template>
  11. <script>
  12. import AppBar from './components/AppBar.vue';
  13. import { computed } from 'vue';
  14. export default {
  15. components: { AppBar },
  16. name: 'App',
  17. provide() {
  18. return {
  19. setKeeps: this.setKeeps,
  20. bar: computed(() => this.barValue),
  21. content: computed(() => this.$refs.content),
  22. scrollWrapper: computed(() => this.$el),
  23. changeScroll: this.changeScroll,
  24. keeps: computed(() => this.keeps),
  25. bodyScroll: computed(() => this.bodyScroll)
  26. };
  27. },
  28. inject: ['barHeight'],
  29. computed: {
  30. barValue() {
  31. return this.$refs.bar;
  32. },
  33. scrollStyle() {
  34. return {
  35. boxSizing: 'border-box',
  36. minHeight: `calc(var(--app-height) - ${(this.barHeight || '0') + 'px'})`
  37. };
  38. }
  39. },
  40. data() {
  41. return {
  42. checkEvent: null,
  43. keeps: [],
  44. bodyScroll: 0
  45. };
  46. },
  47. watch: {
  48. $route() {
  49. this.$nextTick(() => {
  50. this.$el.scrollTop = 0;
  51. });
  52. }
  53. },
  54. methods: {
  55. setKeeps(keep = [], isAdd = true) {
  56. let keeps = [...this.keeps];
  57. if (isAdd) {
  58. keeps = [...keeps, ...keep];
  59. } else {
  60. keeps = keeps.filter(item => {
  61. return !keep.includes(item);
  62. });
  63. }
  64. this.keeps = keeps;
  65. },
  66. changeScroll(scrollTop, isAnimate = false) {
  67. console.log(scrollTop);
  68. if (isAnimate) {
  69. this.$el.scrollTo({
  70. top: scrollTop,
  71. behavior: 'smooth'
  72. });
  73. } else {
  74. this.$el.scrollTop = scrollTop;
  75. }
  76. },
  77. scrollEvent() {
  78. // console.log(this.$el.scrollTop);
  79. this.bodyScroll = this.$el.scrollTop;
  80. }
  81. }
  82. };
  83. </script>
  84. <style lang="less" scoped>
  85. .scroll-content {
  86. min-height: var(--app-height);
  87. box-sizing: border-box;
  88. // padding-top: 46px;
  89. }
  90. .scroll-wrapper {
  91. background-color: @bg;
  92. }
  93. </style>