当前位置:   article > 正文

微信小程序刻度滑块选择组件_微信小程序滑块组件

微信小程序滑块组件

如图,在部分产品需求中会出现刻度滑块选择。例如分数,金钱等等

 

 WXML

 

  1. <!--component/zyslider.wxml-->
  2. <view class="container">
  3. <view class="slider-item min" style="left:{{leftValue}}rpx;"
  4. catchtouchmove="_minMove">
  5. <image src="../../images/all/bz3.png"></image>
  6. <view class="minValue">{{startValue}}</view>
  7. </view>
  8. <view class="slider-item max" style="left:{{rightValue}}rpx;;"
  9. catchtouchmove="_maxMove">
  10. <image src="../../images/all/bz2.png"></image>
  11. <view class="minValue">{{endValue}}</view>
  12. </view>
  13. <view class="slider-body left"
  14. style="display:none;left:0rpx; width:{{leftValue}}rpx;background-color:{{backgroundColor}};"></view>
  15. <view class="slider-body body"
  16. style="display:none;left:{{leftValue}}rpx; width:{{rightValue-leftValue}}rpx;background-color:{{selectedColor}};"></view>
  17. <view class="slider-body right"
  18. style="display:none;left:{{rightValue}}rpx; width:{{totalLength - rightValue}}rpx;background-color:{{backgroundColor}};"></view>
  19. <slot></slot>
  20. </view>

wxss

  1. /* component/zyslider.wxss */
  2. .container {
  3. /*height: 100%;*/
  4. width: 95%;
  5. position: absolute;
  6. left: 5%;
  7. }
  8. .slider-body {
  9. height: 8rpx;
  10. background-color: #EAEDF2;
  11. position: relative;
  12. }
  13. .body {
  14. bottom: 55rpx;
  15. background: linear-gradient(to right, #937EF3, #A6C8FF);
  16. z-index: 0.3;
  17. }
  18. .left {
  19. bottom: 46rpx;
  20. z-index: 0.1;
  21. }
  22. .right {
  23. z-index: 0.2;
  24. bottom: 62rpx;
  25. }
  26. .slider-item {
  27. z-index: 2;
  28. width: 63rpx;
  29. margin-bottom: 10rpx;
  30. }
  31. .slider-item image{
  32. width: 63rpx;
  33. height: 48rpx;
  34. position: relative;
  35. left: -25rpx;
  36. top:-83rpx;
  37. }
  38. .min {
  39. position: relative;
  40. top: 65rpx;
  41. font-size: 24rpx;
  42. text-align: center;
  43. font-family: PingFangSC-Regular, PingFang SC;
  44. font-weight: 400;
  45. color: #F90000;
  46. /* left: 100rpx; */
  47. }
  48. .slider-item .minValue{
  49. width: 63rpx;
  50. height: 48rpx;
  51. position: absolute;
  52. left: -25rpx;
  53. top:-80rpx;
  54. }
  55. .max {
  56. position: relative;
  57. /*left: 600rpx;*/
  58. font-size: 24rpx;
  59. text-align: center;
  60. font-family: PingFangSC-Regular, PingFang SC;
  61. font-weight: 400;
  62. color: #0085F9;
  63. }
  64. .hide{
  65. display: none;
  66. }

 js

  1. // component/zyslider/zyslider.js
  2. var util = require('../../utils/util')
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. /** slider 最小值 */
  9. min: {
  10. type: Number
  11. },
  12. /** slider 最大值 */
  13. max: {
  14. type: Number
  15. },
  16. startValue: {
  17. type: Number
  18. },
  19. endValue: {
  20. type: Number
  21. },
  22. /** 预选选择的小值*/
  23. minValue: {
  24. type: Number
  25. },
  26. /** 预选选择的大值 */
  27. maxValue: {
  28. type: Number
  29. },
  30. /** 滑块颜色 */
  31. blockColor:{
  32. type: String
  33. },
  34. /** 未选择进度条颜色 */
  35. backgroundColor:{
  36. type: String
  37. },
  38. /** 已选择进度条颜色 */
  39. selectedColor:{
  40. type: String
  41. }
  42. },
  43. /**
  44. * 组件的初始数据
  45. */
  46. data: {
  47. min: 300,
  48. max: 750,
  49. leftValue: 0,
  50. rightValue: 0,
  51. totalLength: 750,
  52. bigLength: 750,
  53. ratio:0.5,
  54. sliderLength: 60,
  55. containerLeft: 0, //标识整个组件,距离屏幕左边的距离
  56. },
  57. /**
  58. * 组件的方法列表
  59. */
  60. methods: {
  61. endPop(){
  62. var rightValue=wx.getStorageSync('rightValue')
  63. var leftValue=wx.getStorageSync('leftValue')
  64. if(rightValue){
  65. this.setData({
  66. rightValue:parseInt(rightValue),
  67. })
  68. }
  69. if(leftValue){
  70. this.setData({
  71. leftValue:parseInt(leftValue)
  72. })
  73. }
  74. },
  75. reset(){
  76. let minValue = this.data.minValue / this.data.max * this.data.bigLength
  77. let min = this.data.min / this.data.max * this.data.bigLength
  78. let right = this.data.maxValue / this.data.max * this.data.bigLength + this.data.sliderLength
  79. this.setData({
  80. leftValue: minValue - min,
  81. rightValue: right,
  82. min: 300,
  83. max: 750,
  84. totalLength: 750,
  85. bigLength: 750,
  86. ratio:0.5,
  87. sliderLength: 60,
  88. containerLeft: 0, //标识整个组件,距离屏幕左边的距离
  89. })
  90. },
  91. /**
  92. * 设置左边滑块的值
  93. */
  94. _propertyLeftValueChange: function () {
  95. let minValue = this.data.minValue / this.data.max * this.data.bigLength
  96. let min = this.data.min / this.data.max * this.data.bigLength
  97. this.setData({
  98. leftValue: minValue - min
  99. })
  100. },
  101. /**
  102. * 设置右边滑块的值
  103. */
  104. _propertyRightValueChange: function () {
  105. let right = this.data.maxValue / this.data.max * this.data.bigLength + this.data.sliderLength
  106. this.setData({
  107. rightValue: right
  108. })
  109. },
  110. /**
  111. * 左边滑块滑动
  112. */
  113. _minMove: function (e) {
  114. let pagex = e.changedTouches[0].pageX / this.data.ratio - this.data.containerLeft - this.data.sliderLength / 2
  115. if (pagex + this.data.sliderLength >= this.data.rightValue) {
  116. pagex = this.data.rightValue - this.data.sliderLength
  117. } else if (pagex <= 0) {
  118. pagex = 0
  119. }
  120. this.setData({
  121. leftValue: pagex
  122. })
  123. wx.setStorageSync('leftValue', pagex)
  124. let lowValue = parseInt(pagex / this.data.bigLength * parseInt(this.data.max - this.data.min) + this.data.min)
  125. var myEventDetail = { lowValue: lowValue }
  126. this.triggerEvent('lowValueChange', myEventDetail)
  127. },
  128. /**
  129. * 右边滑块滑动
  130. */
  131. _maxMove: function (e) {
  132. let pagex = e.changedTouches[0].pageX / this.data.ratio - this.data.containerLeft - this.data.sliderLength / 2
  133. if (pagex <= this.data.leftValue + this.data.sliderLength) {
  134. pagex = this.data.leftValue + this.data.sliderLength
  135. } else if (pagex >= this.data.totalLength) {
  136. pagex = this.data.totalLength
  137. }
  138. this.setData({
  139. rightValue: pagex
  140. })
  141. pagex = pagex - this.data.sliderLength
  142. let heighValue = parseInt(pagex / this.data.bigLength * (this.data.max - this.data.min) + this.data.min)
  143. wx.setStorageSync('rightValue', pagex)
  144. var myEventDetail = { heighValue: heighValue }
  145. this.triggerEvent('heighValueChange', myEventDetail)
  146. },
  147. },
  148. ready: function () {
  149. let that = this;
  150. const getSystemInfo = util.wxPromisify(wx.getSystemInfo)
  151. const queryContainer = util.wxPromisify(wx.createSelectorQuery().in(this).select(".container").boundingClientRect)
  152. util.wxPromisify(wx.getSystemInfo)()
  153. .then(res => {
  154. let ratio = res.windowWidth / 750
  155. that.setData({
  156. ratio: ratio,
  157. })
  158. })
  159. .then(() => {
  160. var query = wx.createSelectorQuery().in(this)
  161. query.select(".container").boundingClientRect(function (res) {
  162. that.setData({
  163. totalLength: res.width / that.data.ratio - that.data.sliderLength,
  164. bigLength: res.width / that.data.ratio - that.data.sliderLength * 2,
  165. rightValue: res.width / that.data.ratio - that.data.sliderLength,
  166. containerLeft: res.left / that.data.ratio
  167. })
  168. /**
  169. * 设置初始滑块位置
  170. */
  171. that._propertyLeftValueChange()
  172. that._propertyRightValueChange()
  173. that.endPop()
  174. }).exec()
  175. })
  176. }
  177. })

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/159354
推荐阅读
相关标签
  

闽ICP备14008679号