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.

193 lines
7.0 KiB

2 months ago
  1. /**
  2. * 使用bindingx方案实现slider
  3. * 只能使用于nvue下
  4. */
  5. // 引入bindingx,此库类似于微信小程序wxs,目的是让js运行在视图层,减少视图层和逻辑层的通信折损
  6. const BindingX = uni.requireNativePlugin('bindingx')
  7. // nvue操作dom的库,用于获取dom的尺寸信息
  8. const dom = uni.requireNativePlugin('dom')
  9. // nvue中用于操作元素动画的库,类似于uni.animation,只不过uni.animation不能用于nvue
  10. const animation = uni.requireNativePlugin('animation')
  11. import { range } from '../../libs/function/index';
  12. export default {
  13. data() {
  14. return {
  15. // 位移的偏移量
  16. x: 0,
  17. // 是否正在触摸过程中,用于标记动画类是否添加或移除
  18. touching: false,
  19. changeFromInside: false
  20. }
  21. },
  22. watch: {
  23. // 监听vlaue的变化,此变化可能是由于内部修改v-model的值,或者外部
  24. // 从服务端获取一个值后,赋值给slider的v-model而导致的
  25. value(n) {
  26. if (!this.changeFromInside) {
  27. this.initX()
  28. } else {
  29. this.changeFromInside = false
  30. }
  31. }
  32. },
  33. mounted() {
  34. this.init()
  35. },
  36. methods: {
  37. init() {
  38. // 更新滑块尺寸信息
  39. this.getSliderRect().then((size) => {
  40. this.sliderRect = size
  41. this.initX()
  42. })
  43. },
  44. // 获取节点信息
  45. // 获取slider尺寸
  46. getSliderRect() {
  47. // 获取滑块条的尺寸信息
  48. // 通过nvue的dom模块,查询节点信息
  49. return new Promise((resolve) => {
  50. this.$nextTick(() => {
  51. dom.getComponentRect(this.$refs.slider, (res) => {
  52. resolve(res.size)
  53. })
  54. })
  55. })
  56. },
  57. // 初始化按钮位置
  58. initButtonStyle({
  59. barStyle,
  60. buttonWrapperStyle
  61. }) {
  62. this.barStyle = barStyle
  63. this.buttonWrapperStyle = buttonWrapperStyle
  64. },
  65. emitEvent(event, value) {
  66. this.$emit(event, value || this.value)
  67. },
  68. // 滑动开始
  69. async onTouchStart(e) {
  70. // if (this.disabled) return
  71. // // 阻止页面滚动,可以保证在滑动过程中,不让页面可以上下滚动,造成不好的体验
  72. // e.stopPropagation && e.stopPropagation()
  73. // e.preventDefault && e.preventDefault()
  74. // // 更新滑块的尺寸信息
  75. // this.sliderRect = await this.getSliderRect()
  76. // // 标记滑动过程中触摸点的信息
  77. // this.touchStart(e)
  78. // this.startValue = this.format(this.value)
  79. // this.dragStatus = 'start'
  80. // 标记滑动过程中触摸点的信息
  81. // this.touchStart(e)
  82. },
  83. // 开始滑动
  84. onTouchMove(e) {
  85. // if (this.disabled) return;
  86. // if (this.dragStatus === 'start') {
  87. // this.$emit('drag-start')
  88. // }
  89. // // 标记当前滑动过程中的触点信息,此方法在touch mixin中
  90. // this.touchMove(e)
  91. // this.dragStatus = 'draging'
  92. // const {
  93. // width: sliderWidth
  94. // } = this.sliderRect
  95. // const diff = (this.deltaX / sliderWidth) * this.getRange()
  96. // this.newValue = this.startValue + diff
  97. // this.updateValue(this.newValue, false, true)
  98. // 获取元素ref
  99. // const button = this.$refs['nvue-button'].ref
  100. // const gap = this.$refs['nvue-gap'].ref
  101. // animation.transition(gap, {
  102. // styles: {
  103. // width: `${this.startX + this.deltaX}px`
  104. // }
  105. // })
  106. // // console.log(this.startX + this.deltaX);
  107. // animation.transition(button, {
  108. // styles: {
  109. // transform: `translateX(${this.startX + this.deltaX}px)`
  110. // }
  111. // })
  112. // this.barStyle = {
  113. // width: `${this.startX + this.deltaX}px`
  114. // }
  115. const {
  116. x
  117. } = this.getTouchPoint(e)
  118. this.buttonWrapperStyle = {
  119. transform: `translateX(${x}px)`
  120. }
  121. // this.buttonWrapperStyle = {
  122. // transform: `translateX(${this.format(this.startX + this.deltaX)}px)`
  123. // }
  124. },
  125. // onTouchEnd() {
  126. // if (this.disabled) return;
  127. // if (this.dragStatus === 'draging') {
  128. // this.updateValue(this.newValue, true)
  129. // this.$emit('drag-end');
  130. // }
  131. // },
  132. updateValue(value, end, drag) {
  133. value = this.format(value)
  134. const {
  135. width: sliderWidth
  136. } = this.sliderRect
  137. const width = `${((value - this.min) * sliderWidth) / this.getRange()}`
  138. this.value = value
  139. this.barStyle = {
  140. width: `${width}px`
  141. }
  142. // console.log('width', width);
  143. if (drag) {
  144. this.$emit('drag', {
  145. value
  146. })
  147. }
  148. if (end) {
  149. this.$emit('change', value)
  150. }
  151. if ((drag || end)) {
  152. this.changeFromInside = true
  153. this.$emit('update', value)
  154. }
  155. },
  156. // 从value的变化,倒推得出x的值该为多少
  157. initX() {
  158. const {
  159. left,
  160. width
  161. } = this.sliderRect
  162. // 得出x的初始偏移值,之所以需要这么做,是因为在bindingX中,触摸滑动时,只能的值本次移动的偏移值
  163. // 而无法的值准确的前后移动的两个点的坐标值,weex纯粹为阿里巴巴的KPI(部门业绩考核)产物,也就这样了
  164. this.x = this.value / 100 * width
  165. // 设置移动的值
  166. const barStyle = {
  167. width: `${this.x}px`
  168. }
  169. // 按钮的初始值
  170. const buttonWrapperStyle = {
  171. transform: `translateX(${this.x - this.blockHeight / 2}px)`
  172. }
  173. this.initButtonStyle({
  174. barStyle,
  175. buttonWrapperStyle
  176. })
  177. },
  178. // 移动点占总长度的百分比,此处需要先除以step,是为了保证step大于1时,比如10,那么在滑动11,12px这样的
  179. // 距离时,实际上滑块是不会滑动的,到了16,17px,经过四舍五入后,就变成了20px,进行了下一个跳变
  180. format(value) {
  181. return Math.round(range(this.min, this.max, value) / this.step) * this.step
  182. },
  183. getRange() {
  184. const {
  185. max,
  186. min
  187. } = this
  188. return max - min
  189. }
  190. }
  191. }