u-scroll-list.vue 6.3 KB

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