u-scroll-list.vue 6.2 KB

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