u-scroll-list.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view
  3. class="u-scroll-list"
  4. ref="u-scroll-list"
  5. >
  6. <!-- #ifdef APP-NVUE -->
  7. <!-- nvue使用bindingX实现,以得到更好的性能 -->
  8. <scroller
  9. class="u-scroll-list__scroll-view"
  10. ref="u-scroll-list__scroll-view"
  11. scroll-direction="horizontal"
  12. :show-scrollbar="false"
  13. :offset-accuracy="1"
  14. @scroll="nvueScrollHandler"
  15. >
  16. <view class="u-scroll-list__scroll-view__content">
  17. <slot />
  18. </view>
  19. </scroller>
  20. <!-- #endif -->
  21. <!-- #ifndef APP-NVUE -->
  22. <!-- #ifdef MP-WEIXIN || APP-VUE || H5 || MP-QQ -->
  23. <!-- 以上平台,支持wxs -->
  24. <scroll-view
  25. class="u-scroll-list__scroll-view"
  26. scroll-x
  27. @scroll="wxs.scroll"
  28. @scrolltoupper="wxs.scrolltoupper"
  29. @scrolltolower="wxs.scrolltolower"
  30. :data-scrollWidth="scrollWidth"
  31. :data-barWidth="$u.getPx(indicatorBarWidth)"
  32. :data-indicatorWidth="$u.getPx(indicatorWidth)"
  33. :show-scrollbar="false"
  34. :upper-threshold="0"
  35. :lower-threshold="0"
  36. >
  37. <!-- #endif -->
  38. <!-- #ifndef APP-NVUE || MP-WEIXIN || H5 || APP-VUE || MP-QQ -->
  39. <!-- 非以上平台,只能使用普通js实现 -->
  40. <scroll-view
  41. class="u-scroll-list__scroll-view"
  42. scroll-x
  43. @scroll="scrollHandler"
  44. @scrolltoupper="scrolltoupperHandler"
  45. @scrolltolower="scrolltolowerHandler"
  46. :show-scrollbar="false"
  47. :upper-threshold="0"
  48. :lower-threshold="0"
  49. >
  50. <!-- #endif -->
  51. <view class="u-scroll-list__scroll-view__content">
  52. <slot />
  53. </view>
  54. </scroll-view>
  55. <!-- #endif -->
  56. <view
  57. class="u-scroll-list__indicator"
  58. v-if="indicator"
  59. :style="[$u.addStyle(indicatorStyle)]"
  60. >
  61. <view
  62. class="u-scroll-list__indicator__line"
  63. :style="[lineStyle]"
  64. >
  65. <view
  66. class="u-scroll-list__indicator__line__bar"
  67. :style="[barStyle]"
  68. ref="u-scroll-list__indicator__line__bar"
  69. ></view>
  70. </view>
  71. </view>
  72. </view>
  73. </template>
  74. <!-- #ifndef APP-NVUE || MP-WEIXIN || H5 || APP-VUE || MP-QQ -->
  75. <script
  76. src="./scrollWxs.wxs"
  77. module="wxs"
  78. lang="wxs"
  79. ></script>
  80. <!-- #endif -->
  81. <script>
  82. /**
  83. * scrollList 横向滚动列表
  84. * @description 该组件一般用于同时展示多个商品、分类的场景,也可以完成左右滑动的列表。
  85. * @tutorial https://www.uviewui.com/components/scrollList.html
  86. * @property {String | Number} indicatorWidth 指示器的整体宽度 (默认 50 )
  87. * @property {String | Number} indicatorBarWidth 滑块的宽度 (默认 20 )
  88. * @property {Boolean} indicator 是否显示面板指示器 (默认 true )
  89. * @property {String} indicatorColor 指示器非激活颜色 (默认 '#f2f2f2' )
  90. * @property {String} indicatorActiveColor 指示器的激活颜色 (默认 '#3c9cff' )
  91. * @property {String | Object} indicatorStyle 指示器样式,可通过bottom,left,right进行定位
  92. * @event {Function} left 滑动到左边时触发
  93. * @event {Function} right 滑动到右边时触发
  94. * @example
  95. */
  96. // #ifdef APP-NVUE
  97. const dom = uni.requireNativePlugin('dom')
  98. import nvueMixin from "./nvue.js"
  99. // #endif
  100. import props from './props.js';
  101. export default {
  102. name: 'u-scroll-list',
  103. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  104. // #ifdef APP-NVUE
  105. mixins: [uni.$u.mpMixin, uni.$u.mixin, nvueMixin, props],
  106. // #endif
  107. data() {
  108. return {
  109. scrollInfo: {
  110. scrollLeft: 0,
  111. scrollWidth: 0
  112. },
  113. scrollWidth: 0
  114. }
  115. },
  116. computed: {
  117. // 指示器为线型的样式
  118. barStyle() {
  119. const style = {}
  120. // #ifndef APP-NVUE || MP-WEIXIN || H5 || APP-VUE || MP-QQ
  121. // 此为普通js方案,只有在非nvue和不支持wxs方案的端才使用、
  122. // 此处的计算理由为:scroll-view的滚动距离与目标滚动距离(scroll-view的实际宽度减去包裹元素的宽度)之比,等于滑块当前移动距离与总需
  123. // 滑动距离(指示器的总宽度减去滑块宽度)的比值
  124. const scrollLeft = this.scrollInfo.scrollLeft,
  125. scrollWidth = this.scrollInfo.scrollWidth,
  126. barAllMoveWidth = this.indicatorWidth - this.indicatorBarWidth
  127. const x = scrollLeft / (scrollWidth - this.scrollWidth) * barAllMoveWidth
  128. style.transform = `translateX(${ x }px)`
  129. // #endif
  130. // 设置滑块的宽度和背景色,是每个平台都需要的
  131. style.width = uni.$u.addUnit(this.indicatorBarWidth)
  132. style.backgroundColor = this.indicatorActiveColor
  133. return style
  134. },
  135. lineStyle() {
  136. const style = {}
  137. // 指示器整体的样式,需要设置其宽度和背景色
  138. style.width = uni.$u.addUnit(this.indicatorWidth)
  139. style.backgroundColor = this.indicatorColor
  140. return style
  141. }
  142. },
  143. mounted() {
  144. this.init()
  145. },
  146. methods: {
  147. init() {
  148. this.getComponentWidth()
  149. },
  150. // #ifndef APP-NVUE || MP-WEIXIN || H5 || APP-VUE || MP-QQ
  151. // scroll-view触发滚动事件
  152. scrollHandler(e) {
  153. this.scrollInfo = e.detail
  154. },
  155. scrolltoupperHandler() {
  156. this.scrollEvent('left')
  157. this.scrollInfo.scrollLeft = 0
  158. },
  159. scrolltolowerHandler() {
  160. this.scrollEvent('right')
  161. // 在普通js方案中,滚动到右边时,通过设置this.scrollInfo,模拟出滚动到右边的情况
  162. // 因为上方是用过computed计算的,设置后,会自动调整滑块的位置
  163. this.scrollInfo.scrollLeft = uni.$u.getPx(this.indicatorWidth) - uni.$u.getPx(this.indicatorBarWidth)
  164. },
  165. // #endif
  166. //
  167. scrollEvent(status) {
  168. this.$emit(status)
  169. },
  170. // 获取组件的宽度
  171. async getComponentWidth() {
  172. // 延时一定时间,以获取dom尺寸
  173. await uni.$u.sleep(30)
  174. // #ifndef APP-NVUE
  175. this.$uGetRect('.u-scroll-list').then(size => {
  176. this.scrollWidth = size.width
  177. })
  178. // #endif
  179. // #ifdef APP-NVUE
  180. const ref = this.$refs['u-scroll-list']
  181. ref && dom.getComponentRect(ref, (res) => {
  182. this.scrollWidth = res.size.width
  183. })
  184. // #endif
  185. },
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. @import "../../libs/css/components.scss";
  191. .u-scroll-list {
  192. padding-bottom: 10px;
  193. &__scroll-view {
  194. @include flex;
  195. &__content {
  196. @include flex;
  197. }
  198. }
  199. &__indicator {
  200. @include flex;
  201. justify-content: center;
  202. margin-top: 15px;
  203. &__line {
  204. width: 60px;
  205. height: 4px;
  206. border-radius: 100px;
  207. overflow: hidden;
  208. &__bar {
  209. width: 20px;
  210. height: 4px;
  211. border-radius: 100px;
  212. }
  213. }
  214. }
  215. }
  216. </style>