u-scroll-list.vue 6.3 KB

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