HomeContainer.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="home-container" :style="customHomeStyle" ref="containerRef">
  3. <div class="container" :style="customStyle">
  4. <slot></slot>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. customHomeStyle: {
  12. type: Object,
  13. default: () => ({})
  14. },
  15. customStyle: {
  16. type: Object,
  17. default: () => ({})
  18. }
  19. },
  20. data() {
  21. return {
  22. scrollTimer: null
  23. }
  24. },
  25. mounted() {
  26. this.$nextTick(() => {
  27. this.addScrollListener()
  28. })
  29. },
  30. beforeDestroy() {
  31. this.removeScrollListener()
  32. },
  33. methods: {
  34. addScrollListener() {
  35. const container = this.$refs.containerRef
  36. if (container) {
  37. container.addEventListener('scroll', this.handleScroll)
  38. }
  39. },
  40. removeScrollListener() {
  41. const container = this.$refs.containerRef
  42. if (container) {
  43. container.removeEventListener('scroll', this.handleScroll)
  44. }
  45. if (this.scrollTimer) {
  46. clearTimeout(this.scrollTimer)
  47. }
  48. },
  49. handleScroll() {
  50. // 触发滚动事件
  51. this.$emit('scroll')
  52. // 清除之前的定时器
  53. if (this.scrollTimer) {
  54. clearTimeout(this.scrollTimer)
  55. }
  56. // 设置新的定时器,停止滚动后触发停止事件
  57. this.scrollTimer = setTimeout(() => {
  58. this.$emit('scroll-end')
  59. }, 200)
  60. },
  61. // 滚动到顶部方法
  62. scrollToTop() {
  63. const container = this.$refs.containerRef
  64. if (container) {
  65. container.scrollTo({
  66. top: 0,
  67. behavior: 'smooth'
  68. })
  69. }
  70. }
  71. }
  72. }
  73. </script>
  74. <style lang="scss" scoped>
  75. .home-container {
  76. height: 100%;
  77. overflow: auto;
  78. background: #FFFFFF;
  79. .container {
  80. width: 100%;
  81. padding: 20rpx;
  82. min-height: calc(100vh - 80rpx);
  83. overflow: auto;
  84. box-sizing: border-box;
  85. background: url("../static/images/home-bg.png") no-repeat;
  86. background-size: 100% auto;
  87. }
  88. }
  89. </style>