slider-in-swiper.uvue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="swiper-list">
  3. <scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
  4. <view class="flex-row">
  5. <text ref="swipertab" space="nbsp" class="swiper-tabs-item"
  6. :class="swiperIndex==index ? 'swiper-tabs-item-active' : ''" v-for="(item, index) in swiperList" :key="index"
  7. @click="onTabClick(index)">
  8. {{item.title}}
  9. </text>
  10. </view>
  11. <view ref="indicator" class="swiper-tabs-indicator"></view>
  12. </scroll-view>
  13. <swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
  14. @animationfinish="onSwiperAnimationfinish">
  15. <swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
  16. <text class="uni-title">显示当前value</text>
  17. <slider :value="50" :show-value="true" />
  18. <text class="uni-title">设置步进:step=10跳动</text>
  19. <slider :value="60" :step="10" />
  20. <text class="uni-title">浮点步进:step=0.01跳动</text>
  21. <slider :value="0.5" :min="0" :max="1" :step="0.01" :show-value="true" />
  22. <text class="uni-title">设置最小/最大值</text>
  23. <slider :value="100" :min="50" :max="200" :show-value="true" />
  24. <text class="uni-title">不同颜色和大小的滑块</text>
  25. <slider id="slider-custom-color-and-size" :value="sliderValue" :backgroundColor="sliderBackgroundColor"
  26. :activeColor="sliderActiveColor" :activeBackgroundColor="sliderActiveColor" :blockColor="sliderBlockColor"
  27. :foreColor="sliderBlockColor" :block-size="sliderBlockSize" />
  28. </swiper-item>
  29. </swiper>
  30. </view>
  31. </template>
  32. <script>
  33. type SwiperTabsItem = {
  34. x : number,
  35. w : number
  36. }
  37. type SwiperViewItem = {
  38. title : string,
  39. }
  40. /**
  41. * 根据权重在两个值之间执行线性插值.
  42. * @constructor
  43. * @param {number} value1 - 第一个值,该值应为下限.
  44. * @param {number} value2 - 第二个值,该值应为上限.
  45. * @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
  46. * @returns {number} 内插值
  47. */
  48. function lerpNumber(value1 : number, value2 : number, amount : number) : number {
  49. return (value1 + (value2 - value1) * amount)
  50. }
  51. export default {
  52. data() {
  53. return {
  54. swiperList: [] as SwiperViewItem[],
  55. swiperIndex: 0,
  56. tabScrollView: null as null | UniElement,
  57. indicatorNode: null as null | UniElement,
  58. animationFinishIndex: 0,
  59. swiperWidth: 0,
  60. swiperTabsRect: [] as SwiperTabsItem[],
  61. sliderValue: 50,
  62. sliderBlockSize: 20,
  63. sliderBackgroundColor: "#000000",
  64. sliderActiveColor: "#FFCC33",
  65. sliderBlockColor: "#8A6DE9",
  66. }
  67. },
  68. onLoad() {
  69. // 初始化 5 个页签,宽度依次递增,用于演示在滑动 swiper 过程中指示线宽度的线性变化
  70. for (let i = 0; i < 3; i++) {
  71. const space = " ".repeat(i)
  72. this.swiperList.push({
  73. title: "Tab " + space + i
  74. } as SwiperViewItem)
  75. }
  76. },
  77. onReady() {
  78. this.tabScrollView = this.$refs['tabScroll'] as UniElement;
  79. this.indicatorNode = this.$refs['indicator'] as UniElement;
  80. (this.$refs["swiper"] as UniElement).getBoundingClientRectAsync()!.then((res : DOMRect) : Promise<void> => {
  81. this.swiperWidth = res.width
  82. return this.cacheTabItemsSize()
  83. }).then(() => {
  84. this.updateTabIndicator(this.swiperIndex, this.swiperIndex, 1)
  85. });
  86. },
  87. methods: {
  88. /**
  89. * 每个页签的点击事件,点击后设置当前 swiper.
  90. * @constructor
  91. * @param {number} index - 当前索引,该值应介于 0 和 最大数量之间.
  92. */
  93. onTabClick(index : number) {
  94. this.setSwiperIndex(index, false)
  95. },
  96. onSwiperTransition(e : SwiperTransitionEvent) {
  97. // 微信 skyline 每项完成触发 Animationfinish,偏移值重置
  98. // 微信 webview 全部完成触发 Animationfinish,偏移值累加
  99. // 在滑动到下一个项的过程中,再次反向滑动,偏移值递减
  100. // uni-app-x 和 微信 webview 行为一致
  101. const offset_x = e.detail.dx
  102. // 计算当前索引并重置差异
  103. const current_offset_x = offset_x % this.swiperWidth
  104. const current_offset_i = offset_x / this.swiperWidth
  105. const current_index = this.animationFinishIndex + parseInt(current_offset_i + '')
  106. // 计算目标索引及边界检查
  107. let move_to_index = current_index
  108. if (current_offset_x > 0 && move_to_index < this.swiperList.length - 1) {
  109. move_to_index += 1
  110. } else if (current_offset_x < 0 && move_to_index > 0) {
  111. move_to_index -= 1
  112. }
  113. // 计算偏移百分比
  114. const percentage = Math.abs(current_offset_x) / this.swiperWidth
  115. // 通知更新指示线
  116. if (current_index != move_to_index) {
  117. this.updateTabIndicator(current_index, move_to_index, percentage)
  118. }
  119. },
  120. /**
  121. * Swiper滑动动画结束事件.
  122. */
  123. onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
  124. this.setSwiperIndex(e.detail.current, true)
  125. // 记录上次的索引位置
  126. this.animationFinishIndex = e.detail.current
  127. },
  128. /**
  129. * 缓存所有页签的左边距和宽度,用于计算指示线在移动过程中的线性变化.
  130. */
  131. async cacheTabItemsSize() {
  132. this.swiperTabsRect.length = 0;
  133. const tabs = this.$refs["swipertab"] as UniElement[]
  134. for (let i = 0; i < tabs.length; i++) {
  135. const element = tabs[i];
  136. // #ifdef MP
  137. const rect = await element.getBoundingClientRectAsync()!
  138. const x = rect.left
  139. const w = rect.width
  140. // #endif
  141. // #ifndef MP
  142. const x = element.offsetLeft
  143. const w = element.offsetWidth
  144. // #endif
  145. this.swiperTabsRect.push({
  146. x,
  147. w
  148. } as SwiperTabsItem)
  149. }
  150. },
  151. /**
  152. * 设置当前 swiper 选中的索引值.
  153. * @constructor
  154. * @param {number} index - 当前索引,该值应介于 0 和 最大数量之间.
  155. * @param {boolean} updateIndicator - 是否更新指示线.
  156. */
  157. setSwiperIndex(index : number, updateIndicator : boolean) {
  158. if (this.swiperIndex === index) {
  159. return
  160. }
  161. this.swiperIndex = index
  162. if (updateIndicator) {
  163. this.updateTabIndicator(index, index, 1)
  164. }
  165. },
  166. /**
  167. * 更新页签指示线的位置和宽度.
  168. * @constructor
  169. * @param {number} current_index - 当前索引,该值应介于 0 和 最大数量之间.
  170. * @param {number} move_to_index - 目标索引,该值应介于 0 和 最大数量之间.
  171. * @param {number} percentage - 偏移百分比,应介于 0 和 1 之间,用于计算在移动过程中的线性值.
  172. */
  173. updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
  174. const current_size = this.swiperTabsRect[current_index]
  175. const move_to_size = this.swiperTabsRect[move_to_index]
  176. // 计算指示线 位置 和 宽度 在移动过程中的线性值
  177. const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
  178. const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
  179. // 通过 transform 更新指示线,避免重排版
  180. // #ifdef APP
  181. const x = indicator_line_x + indicator_line_w / 2
  182. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
  183. // #endif
  184. // #ifdef WEB || MP
  185. // TODO chrome windows系统 transform scaleX渲染bug
  186. const x = indicator_line_x
  187. this.indicatorNode?.style?.setProperty('width', `${indicator_line_w}px`)
  188. this.indicatorNode?.style?.setProperty('transform', `translateX(${x}px)`)
  189. // #endif
  190. // 滚动到水平中心位置
  191. const scroll_x = x - this.swiperWidth / 2
  192. // app 平台后续支持 scrollTo()
  193. // #ifndef MP-WEIXIN
  194. this.tabScrollView!.scrollLeft = scroll_x
  195. // #endif
  196. // #ifdef MP-WEIXIN
  197. this.tabScrollView!.scrollTo({
  198. left: scroll_x
  199. })
  200. // #endif
  201. }
  202. }
  203. }
  204. </script>
  205. <style>
  206. .flex-row {
  207. flex-direction: row;
  208. align-self: flex-start;
  209. }
  210. .swiper-list {
  211. flex: 1;
  212. }
  213. .swiper-tabs {
  214. background-color: #ffffff;
  215. }
  216. .swiper-tabs-item {
  217. color: #555;
  218. font-size: 16px;
  219. padding: 12px 25px;
  220. white-space: nowrap;
  221. }
  222. .swiper-tabs-item-active {
  223. color: #007AFF;
  224. }
  225. .swiper-tabs-indicator {
  226. width: 1px;
  227. height: 2px;
  228. background-color: #007AFF;
  229. }
  230. .swiper-view {
  231. flex: 1;
  232. }
  233. .swiper-item {
  234. flex: 1;
  235. padding: 15px;
  236. }
  237. </style>