You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.2 KiB

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