index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // 组件
  2. import ibpsContainerFull from './components/ibps-container-full.vue'
  3. import ibpsContainerFullBs from './components/ibps-container-full-bs.vue'
  4. import ibpsContainerGhost from './components/ibps-container-ghost.vue'
  5. import ibpsContainerGhostBs from './components/ibps-container-ghost-bs.vue'
  6. import ibpsContainerCard from './components/ibps-container-card.vue'
  7. import ibpsContainerCardBs from './components/ibps-container-card-bs.vue'
  8. export default {
  9. name: 'ibps-container',
  10. props: {
  11. // 容器样式
  12. type: {
  13. type: String,
  14. required: false,
  15. default: 'full'
  16. },
  17. // 滚动优化
  18. betterScroll: {
  19. type: Boolean,
  20. required: false,
  21. default: false
  22. },
  23. marginLeft: {
  24. type: String
  25. },
  26. marginRight: {
  27. type: String
  28. }
  29. },
  30. computed: {
  31. // 始终返回渲染组件
  32. component() {
  33. if (this.type === 'card' && !this.betterScroll) return ibpsContainerCard
  34. if (this.type === 'card' && this.betterScroll) return ibpsContainerCardBs
  35. if (this.type === 'ghost' && !this.betterScroll) return ibpsContainerGhost
  36. if (this.type === 'ghost' && this.betterScroll) return ibpsContainerGhostBs
  37. if (this.type === 'full' && !this.betterScroll) return ibpsContainerFull
  38. if (this.type === 'full' && this.betterScroll) return ibpsContainerFullBs
  39. else {
  40. return 'div'
  41. }
  42. }
  43. },
  44. render(h) {
  45. const slots = [
  46. this.$slots.default,
  47. this.$slots.header ? <template slot='header'>{ this.$slots.header }</template> : null,
  48. this.$slots.footer ? <template slot='footer'>{ this.$slots.footer }</template> : null
  49. ]
  50. // 增加左右布局
  51. const style = {
  52. marginRight: this.marginRight,
  53. marginLeft: this.marginLeft
  54. }
  55. return <div
  56. ref='container'
  57. class='container-component'
  58. style= {style }
  59. >
  60. <this.component
  61. ref='component'
  62. { ...{ attrs: this.$attrs } }
  63. onScroll={ e => this.$emit('scroll', e) }>
  64. { slots }
  65. </this.component>
  66. </div>
  67. },
  68. methods: {
  69. // 返回顶部
  70. scrollToTop() {
  71. this.$refs.component.scrollToTop()
  72. // 如果开启了 better scroll 还需要手动触发一遍 scroll 事件
  73. const bs = this.$refs.component.BS
  74. if (bs) this.$refs.component.scroll()
  75. },
  76. // 用法同原生方法 scrollBy
  77. scrollBy(x = 0, y = 0, time = 300) {
  78. if (this.betterScroll) {
  79. const bs = this.$refs.component.BS
  80. if (bs) {
  81. bs.scrollBy(-x, -y, time)
  82. // 手动触发一遍 scroll 事件
  83. this.$refs.component.scroll()
  84. }
  85. } else {
  86. this.$refs.component.$refs.body.scrollBy(x, y)
  87. }
  88. },
  89. // 用法同原生方法 scrollTo
  90. scrollTo(x = 0, y = 0, time = 300) {
  91. if (this.betterScroll) {
  92. const bs = this.$refs.component.BS
  93. if (bs) {
  94. bs.scrollTo(-x, -y, time)
  95. // 手动触发一遍 scroll 事件
  96. this.$refs.component.scroll()
  97. }
  98. } else {
  99. this.$refs.component.$refs.body.scrollTo(x, y)
  100. }
  101. },
  102. // 用法同原生方法 scrollTop
  103. scrollTop(top = 0, time = 300) {
  104. if (this.betterScroll) {
  105. const bs = this.$refs.component.BS
  106. if (bs) {
  107. bs.scrollTo(bs.x, -top, time)
  108. // 手动触发一遍 scroll 事件
  109. this.$refs.component.scroll()
  110. }
  111. } else {
  112. this.$refs.component.$refs.body.scrollTop = top
  113. }
  114. }
  115. }
  116. }