当前位置:   article > 正文

微信小程序双向滑动slider_uniapp slider 双向滑动

uniapp slider 双向滑动

1.使用场景,通过滑动滑块,获取价格的范围,提交表单获取搜索结果。slider是单向的,这种范围获取的可能不行(当时的想法),还有一种思路,就是通过touch事件,使用view进行布局,手写一个(这种当然可以)。然后再网上找了一些资料,发现有人也有相同的需求,而且有案例,然后就做了伸手党,并且把找到的资料做了一下整理,方便后续参考使用,如有侵权,请联系博主删除。

2.代码

2.1wxml

  1. <view class='buyMian-title'>预算</view>
  2. <view class='buyMian-slide'>
  3. <view class='buyMian-slide-title'>{{min}}万-{{max}}万</view>
  4. <view class='buyMian-slide-contain'>
  5. <slider style='width:{{slider1W+"%"}}' class='slider-left' min='{{min}}' max='{{slider1Max}}' backgroundColor='#ff7500' activeColor='#f7f7f7' bindchanging='changing' catchtouchstart='changeStart' bindchange='changed' data-idx='1'></slider>
  6. <slider wx:if='{{!change}}' style='width:{{slider2W+"%"}}' class='slider-right' min='{{slider2Min}}' max='{{max}}' backgroundColor='#f7f7f7' activeColor='#ff7500' bindchanging='changing' catchtouchstart='changeStart' bindchange='changed' data-idx='2'></slider>
  7. </view>
  8. <view class='flex flexSb buyMian-slide-range'>
  9. <view class=''>{{slider1Value}}万</view>
  10. <view class=''>{{slider2Value}}万</view>
  11. </view>
  12. </view>

2.2wxss

  1. /* slide */
  2. .buyMian-slide, .buyMian-title {
  3. width: 654rpx;
  4. margin: 0 auto;
  5. }
  6. .buyMian-title {
  7. font-size: 34rpx;
  8. color: #212121;
  9. line-height: 36rpx;
  10. margin-top: 45rpx;
  11. }
  12. .buyMian-slide-title {
  13. font-size: 36rpx;
  14. color: #ff7500;
  15. line-height: 38rpx;
  16. margin-top: 25rpx;
  17. text-align: center;
  18. }
  19. .buyMian-slide-slide {
  20. margin: 10rpx auto 0;
  21. }
  22. /* range */
  23. .buyMian-slide-contain {
  24. width: 100%;
  25. display: flex;
  26. padding: 20px 0 10px;
  27. position: relative;
  28. left: -25rpx;
  29. }
  30. .buyMian-slide-range {
  31. font-size: 26rpx;
  32. color: #8a8a8a;
  33. margin-top: 15rpx;
  34. }
  35. .slider-left, .slider-right {
  36. margin-right: -8rpx;
  37. }
  38. .flex {
  39. display: -webkit-box;
  40. display: -moz-box;
  41. display: -ms-flexbox;
  42. display: -webkit-flex;
  43. display: flex;
  44. display: box;
  45. flex-wrap: wrap;
  46. }
  47. .flexSb {
  48. justify-content: space-between;
  49. -webkit-justify-content: space-between;
  50. -moz-justify-content: space-between;
  51. -ms-justify-content: space-between;
  52. -o-justify-content: space-between;
  53. }

2.3js

  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. change: false, // 当两个slider在最右端重合时,将change设置为true,从而隐藏slider2,才能继续操作slider1
  7. max: 1000, // 两个slider所能达到的最大值
  8. min: 50, // 两个slider所能取的最小值
  9. rate: 9.5, // slider的最大最小值之差和100(或1000)之间的比率
  10. slider1Max: 1000, // slider1的最大取值
  11. slider1Value: 50, // slider1的值
  12. slider2Value: 1000, // slider2的值
  13. slider2Min: 50, // slider2的最小取值
  14. slider1W: 100, // slider1的宽度
  15. slider2W: 0, // slider2的宽度
  16. },
  17. // 开始滑动
  18. changeStart: function (e) {
  19. var idx = parseInt(e.currentTarget.dataset.idx)
  20. if (idx === 1) {
  21. // dW是当前操作的slider所能占据的最大宽度百分数
  22. var dW = (this.data.slider2Value - this.data.min) / this.data.rate
  23. this.setData({
  24. slider1W: dW,
  25. slider2W: 100 - dW,
  26. slider1Max: this.data.slider2Value,
  27. slider2Min: this.data.slider2Value,
  28. change: false
  29. })
  30. } else if (idx === 2) {
  31. var dw = (this.data.max - this.data.slider1Value) / this.data.rate
  32. this.setData({
  33. slider2W: dw,
  34. slider1W: 100 - dw,
  35. slider1Max: this.data.slider1Value,
  36. slider2Min: this.data.slider1Value,
  37. change: false
  38. })
  39. }
  40. },
  41. // 正在滑动
  42. changing: function (e) {
  43. var idx = parseInt(e.currentTarget.dataset.idx)
  44. var value = e.detail.value
  45. if (idx === 1) {
  46. this.setData({
  47. slider1Value: value
  48. })
  49. } else if (idx === 2) {
  50. this.setData({
  51. slider2Value: value,
  52. })
  53. }
  54. },
  55. changed: function (e) {
  56. console.log(e)
  57. if (this.data.slider1Value === this.data.slider2Value && this.data.slider2Value === this.data.max) {
  58. this.setData({
  59. change: true
  60. })
  61. }
  62. },
  63. })

2.4效果

3.说明

3.1转载地址:https://blog.csdn.net/Hero_rong/article/details/101057530

3.2推荐地址:https://www.jianshu.com/p/7eaf95d1ae1f

3.3推荐地址:https://github.com/weixianlove/zy-slider (这个在git上有组件源码,拉下来可直接使用)

3.4推荐地址:https://blog.csdn.net/qq_35324453/article/details/81952794 (这个有点瑕疵,可以看下)

3.5推荐地址:https://blog.csdn.net/huangyuxin_/article/details/79507114  (这个其实讲的是api的使用,能把线条改为彩色的,也可看看)

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

闽ICP备14008679号