scroll.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <div ref="wrapper">
  3. <slot></slot>
  4. </div>
  5. </template>
  6. <script type="text/ecmascript-6">
  7. import BScroll from 'better-scroll'
  8. export default {
  9. props: {
  10. probeType: {
  11. type: Number,
  12. default: 3
  13. },
  14. click: {
  15. type: Boolean,
  16. default: true
  17. },
  18. data: {
  19. type: Array,
  20. default: null
  21. },
  22. listenScroll: {
  23. type: Boolean,
  24. default: false
  25. },
  26. pullup: {
  27. type: Boolean,
  28. default: false
  29. },
  30. scrollEnd: {
  31. type: Boolean,
  32. default: false
  33. }
  34. },
  35. mounted() {
  36. setTimeout(() => {
  37. this._initScroll()
  38. }, 20)
  39. },
  40. methods: {
  41. _initScroll() {
  42. if (!this.$refs.wrapper) {
  43. return
  44. }
  45. this.scroll = new BScroll(this.$refs.wrapper, {
  46. probeType: this.probeType,
  47. click: this.click
  48. })
  49. if (this.listenScroll) {
  50. let me = this
  51. this.scroll.on('scroll', pos => {
  52. me.$emit('scroll', pos)
  53. })
  54. }
  55. if (this.pullup) {
  56. this.scroll.on('scrollEnd', () => {
  57. if (this.scroll.y <= this.scroll.maxScrollY + 50) {
  58. this.$emit('scrollToEnd')
  59. }
  60. })
  61. }
  62. if (this.scrollEnd) {
  63. this.scroll.on('scrollEnd', () => {
  64. this.$emit('scrollToEnd')
  65. })
  66. }
  67. },
  68. enable() {
  69. this.scroll && this.scroll.enable()
  70. },
  71. disable() {
  72. this.scroll && this.scroll.disable()
  73. },
  74. refresh() {
  75. this.scroll && this.scroll.refresh()
  76. },
  77. scrollTo() {
  78. this.scroll && this.scroll.scrollTo.apply(this.scroll, arguments)
  79. },
  80. scrollToElement() {
  81. this.scroll && this.scroll.scrollToElement.apply(this.scroll, arguments)
  82. }
  83. },
  84. watch: {
  85. data() {
  86. setTimeout(() => {
  87. this.refresh()
  88. }, 20)
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="sass" type="text/sass">
  94. </style>