综合办公系统
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

uv-scroll-list.vue 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <view
  3. class="uv-scroll-list"
  4. ref="uv-scroll-list"
  5. >
  6. <!-- #ifdef APP-NVUE -->
  7. <!-- nvue使用bindingX实现,以得到更好的性能 -->
  8. <scroller
  9. class="uv-scroll-list__scroll-view"
  10. ref="uv-scroll-list__scroll-view"
  11. scroll-direction="horizontal"
  12. :show-scrollbar="false"
  13. :offset-accuracy="1"
  14. @scroll="nvueScrollHandler"
  15. >
  16. <view class="uv-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="uv-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="$uv.getPx(indicatorBarWidth)"
  32. :data-indicatorWidth="$uv.getPx(indicatorWidth)"
  33. :show-scrollbar="false"
  34. :upper-threshold="0"
  35. :lower-threshold="0"
  36. :data-unit="$uv.unit"
  37. >
  38. <!-- #endif -->
  39. <!-- #ifndef APP-NVUE || MP-WEIXIN || H5 || APP-VUE || MP-QQ -->
  40. <!-- 非以上平台,只能使用普通js实现 -->
  41. <scroll-view
  42. class="uv-scroll-list__scroll-view"
  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="uv-scroll-list__scroll-view__content">
  53. <slot />
  54. </view>
  55. </scroll-view>
  56. <!-- #endif -->
  57. <view
  58. class="uv-scroll-list__indicator"
  59. v-if="indicator"
  60. :style="[$uv.addStyle(indicatorStyle)]"
  61. >
  62. <view
  63. class="uv-scroll-list__indicator__line"
  64. :style="[lineStyle]"
  65. >
  66. <view
  67. class="uv-scroll-list__indicator__line__bar"
  68. :style="[barStyle]"
  69. ref="uv-scroll-list__indicator__line__bar"
  70. ></view>
  71. </view>
  72. </view>
  73. <!-- 避免报错 -->
  74. <!-- #ifdef H5 -->
  75. <view v-else class="uv-scroll-list__indicator__line__bar"></view>
  76. <!-- #endif -->
  77. </view>
  78. </template>
  79. <script src="./scrollWxs.wxs" module="wxs" lang="wxs"></script>
  80. <script>
  81. /**
  82. * scrollList 横向滚动列表
  83. * @description 该组件一般用于同时展示多个商品、分类的场景,也可以完成左右滑动的列表。
  84. * @tutorial https://www.uviewui.com/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 mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  100. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  101. import props from './props.js';
  102. export default {
  103. name: 'uv-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) * this.$uv.getPx(this.$uv.addUnit(barAllMoveWidth))
  131. style.transform = `translateX(${ x }px)`
  132. // #endif
  133. // 设置滑块的宽度和背景色,是每个平台都需要的
  134. style.width = this.$uv.addUnit(this.indicatorBarWidth)
  135. style.backgroundColor = this.indicatorActiveColor
  136. return style
  137. },
  138. lineStyle() {
  139. const style = {}
  140. // 指示器整体的样式,需要设置其宽度和背景色
  141. style.width = this.$uv.addUnit(this.indicatorWidth)
  142. style.backgroundColor = this.indicatorColor
  143. return style
  144. }
  145. },
  146. mounted() {
  147. this.init()
  148. },
  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 = this.$uv.getPx(this.indicatorWidth) - this.$uv.getPx(this.indicatorBarWidth)
  167. },
  168. // #endif
  169. scrollEvent(status) {
  170. this.$emit(status)
  171. },
  172. // 获取组件的宽度
  173. async getComponentWidth() {
  174. // 延时一定时间,以获取dom尺寸
  175. await this.$uv.sleep(30)
  176. // #ifndef APP-NVUE
  177. this.$uvGetRect('.uv-scroll-list').then(size => {
  178. this.scrollWidth = size.width
  179. })
  180. // #endif
  181. // #ifdef APP-NVUE
  182. const ref = this.$refs['uv-scroll-list']
  183. ref && dom.getComponentRect(ref, (res) => {
  184. this.scrollWidth = res.size.width
  185. })
  186. // #endif
  187. },
  188. }
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  193. .uv-scroll-list {
  194. padding-bottom: 10px;
  195. &__scroll-view {
  196. @include flex;
  197. &__content {
  198. @include flex;
  199. }
  200. }
  201. &__indicator {
  202. @include flex;
  203. justify-content: center;
  204. margin-top: 15px;
  205. &__line {
  206. width: 60px;
  207. height: 4px;
  208. border-radius: 100px;
  209. overflow: hidden;
  210. &__bar {
  211. width: 20px;
  212. height: 4px;
  213. border-radius: 100px;
  214. }
  215. }
  216. }
  217. }
  218. </style>