App.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="scroll-wrapper" id="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. <phone-ver></phone-ver>
  10. </div>
  11. </template>
  12. <script>
  13. import AppBar from './components/AppBar.vue';
  14. import { computed } from 'vue';
  15. import PhoneVer from './components/PhoneVer.vue';
  16. export default {
  17. components: { AppBar, PhoneVer },
  18. name: 'App',
  19. provide() {
  20. return {
  21. setKeeps: this.setKeeps,
  22. bar: computed(() => this.barValue),
  23. content: computed(() => this.$refs.content),
  24. scrollWrapper: computed(() => this.$el),
  25. changeScroll: this.changeScroll,
  26. keeps: computed(() => this.keeps),
  27. bodyScroll: computed(() => this.bodyScroll),
  28. changeTab: this.changeTab
  29. };
  30. },
  31. inject: ['barHeight', 'appHeight'],
  32. computed: {
  33. barValue() {
  34. return this.$refs.bar;
  35. },
  36. scrollStyle() {
  37. return {
  38. boxSizing: 'border-box',
  39. minHeight: this.fullPage
  40. ? this.appHeight
  41. : `calc(${this.appHeight} - ${(this.barHeight || '0') + 'px'})`
  42. };
  43. }
  44. },
  45. data() {
  46. return {
  47. checkEvent: null,
  48. keeps: [],
  49. bodyScroll: 0,
  50. fullPage: false
  51. };
  52. },
  53. watch: {
  54. $route() {
  55. this.$nextTick(() => {
  56. this.$el.scrollTop = 0;
  57. });
  58. this.checkFull();
  59. }
  60. },
  61. mounted() {
  62. this.checkFull();
  63. },
  64. methods: {
  65. checkFull() {
  66. if (this.$route.meta.fullPage) {
  67. this.fullPage = true;
  68. } else {
  69. this.fullPage = false;
  70. }
  71. },
  72. setKeeps(keep = [], isAdd = true, isClear = false) {
  73. let keeps = [...this.keeps];
  74. if (isAdd) {
  75. keeps = [...keeps, ...keep];
  76. } else {
  77. keeps = keeps.filter(item => {
  78. return !keep.includes(item);
  79. });
  80. }
  81. if (isClear) {
  82. keeps = [];
  83. }
  84. this.keeps = keeps;
  85. },
  86. changeScroll(scrollTop, isAnimate = false) {
  87. console.log(scrollTop);
  88. if (isAnimate) {
  89. this.$el.scrollTo({
  90. top: scrollTop || 0,
  91. behavior: 'smooth'
  92. });
  93. } else {
  94. this.$el.scrollTop = scrollTop || 0;
  95. }
  96. },
  97. scrollEvent() {
  98. // console.log(this.$el.scrollTop);
  99. this.bodyScroll = this.$el.scrollTop;
  100. },
  101. changeTab(color) {
  102. this.$refs.bar.getColor(color);
  103. }
  104. }
  105. };
  106. </script>
  107. <style lang="less" scoped>
  108. .scroll-content {
  109. box-sizing: border-box;
  110. // padding-top: 46px;
  111. }
  112. .scroll-wrapper {
  113. background-color: @bg;
  114. }
  115. </style>