ListScroll.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div ref="wrapper" class="scroll-wrapper">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script>
  7. import BScroll from 'better-scroll'
  8. export default {
  9. props: {
  10. /**
  11. * 1 滚动的时候会派发scroll事件,会截流。
  12. * 2 滚动的时候实时派发scroll事件,不会截流。
  13. * 3 除了实时派发scroll事件,在swipe的情况下仍然能实时派发scroll事件
  14. */
  15. probeType: {
  16. type: Number,
  17. default: 1
  18. },
  19. // 点击列表是否派发click事件
  20. click: {
  21. type: Boolean,
  22. default: true
  23. },
  24. // 是否开启横向滚动
  25. scrollX: {
  26. type: Boolean,
  27. default: false
  28. },
  29. // 是否派发滚动事件
  30. listenScroll: {
  31. type: Boolean,
  32. default: false
  33. },
  34. // 列表的数据
  35. scrollData: {
  36. type: Array,
  37. default: null
  38. },
  39. // 是否派发滚动到底部的事件,用于上拉加载
  40. pullup: {
  41. type: Boolean,
  42. default: false
  43. },
  44. // 是否派发顶部下拉的事件,用于下拉刷新
  45. pulldown: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 是否派发列表滚动开始的事件
  50. beforeScroll: {
  51. type: Boolean,
  52. default: false
  53. },
  54. // 当数据更新后,刷新scroll的延时
  55. refreshDelay: {
  56. type: Number,
  57. default: 20
  58. }
  59. },
  60. mounted() {
  61. // 在 DOM 渲染完毕后初始化 better-scroll
  62. this.$nextTick(() => {
  63. this.initScroll()
  64. })
  65. },
  66. updated() {
  67. this.bs.refresh()
  68. },
  69. methods: {
  70. initScroll() {
  71. // better-scroll 初始化
  72. this.bs = new BScroll(this.$refs.wrapper, {
  73. probeType: 3,
  74. click: true
  75. })
  76. this.bs.on('scroll', () => {
  77. console.log('scrolling-')
  78. })
  79. this.bs.on('scrollEnd', () => {
  80. console.log('scrollingEnd')
  81. })
  82. }
  83. }
  84. }
  85. </script>
  86. <style lang="less" scoped>
  87. .scroll-wrapper {
  88. width: 100%;
  89. height: 100%;
  90. overflow: hidden;
  91. overflow-y: scroll;
  92. touch-action: pan-y;
  93. }
  94. </style>