我是靠谱客的博主 苹果冰棍,这篇文章主要介绍小程序中多滑块的实现代码,现在分享给大家,希望可以做个参考。

本篇文章给大家带来的内容是关于小程序中多滑块的实现代码 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

最近在用原生的代码开发小程序,需要用到多滑块的slider,但是官方的api只支持单滑块,所以就在原来的基础上草草的写了一个。有什么不足的地方还请大家多多指教,想封装成组件的也可自行封装,我这就不讲了。;

话不多说,上代码:

html:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
<view class='sliderHCon'> <view class='showMoney'> <text class='MoneyValue'>¥{{leftShowValue}}</text> <text class='MoneyValue'>¥{{rightShowValue}}</text> </view> <view class='twoSlider'> <slider class='slider-left' min='{{Min}}' max='{{Max}}' value='{{leftValue}}' activeColor='#ccc' backgroundColor='#ccc' block-size='{{blockSize}}' step='{{step}}' bindchanging="leftChange" rightChange='leftChange'> <em class='slider-bg' style='left:{{setSliderLeftX}};width:{{setSliderWidthX}}'></em> </slider> <slider class='slider-right' min='{{Min}}' max='{{Max}}' value='{{rightValue}}' activeColor='#ccc' backgroundColor='#ccc' block-size='{{blockSize}}' step='{{step}}' bindchanging="rightChange" bindchange='rightChange'/> </view> </view>
登录后复制

css

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.sliderHCon { height: 250rpx; width: 100%; margin: auto; display: flex; justify-content: center; align-items: center; flex-direction: column; } .MoneyValue { font-size: 30rpx; text-align: center; color: #999; margin-top: 15rpx; } .showMoney text { margin-right: 30rpx; } .twoSlider { width: 100%; height:100px; display: flex; flex-direction: row; justify-content: center; align-items: center; position: relative; } .slider-left,.slider-right{position: absolute;left:0;right:0;} .slider-bg{position: absolute;top:50%;margin-top:-1px;left:0;width:100%;height:2px;background: blue;z-index: 9;}
登录后复制

js

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
data: { blockSize:20, step:10, Min: 0, //最小值 Max: 1000, //最大值 leftValue: 0, //左边滑块默认值 rightValue: 1000, //右边滑块默认值 leftShowValue: 0, //界面显示左边滑块默认值 rightShowValue: 1000, //界面显示右边滑块默认值 leftWidth: '50', //左边滑块可滑动长度:百分比 rightWidth: '50', //右边滑块可滑动长度:百分比 sliderWidth:0, // slider的宽度; setSliderLeftX: 0, // 设置的sliderp的left setSliderWidthX: 0// 设置的sliderp的width }, onLoad(options) { var query = wx.createSelectorQuery(); // 如果是封装的组件的话,这边请注意写法不同哦; query.select('.slider-left').boundingClientRect((rect) => { this.setData({ sliderWidth: rect.width, setSliderLeftX: (rect.width / this.data.Max * this.data.leftValue) + this.data.blockSize/2 + 'px', setSliderWidthX: rect.width / this.data.Max * (this.data.rightValue - this.data.leftValue) - this.data.blockSize + 'px', }) }).exec(); }, // 左边滑块滑动的值 leftChange(e){ var that = this; that.setData({ leftValue: e.detail.value //设置左边当前值 }) this.setSliderBgColor(e,'left'); }, // 右边滑块滑动的值 rightChange: function (e) { var that = this; that.setData({ rightValue: e.detail.value, }) this.setSliderBgColor(e, 'right'); }, setSliderBgColor(e, type){ if (type == 'left') { // 左边 if (this.data.leftValue < this.data.rightValue) { console.log('拖左不超右边'); this.setData({ leftShowValue: e.detail.value, }) this.setData({ rightShowValue: this.data.rightValue, }) } else { console.log('拖左超右边'); this.setData({ leftShowValue: this.data.rightValue, }) this.setData({ rightShowValue: e.detail.value, }) } } else { // 右边 if (this.data.leftValue < this.data.rightValue) { console.log('拖右不超右边'); this.setData({ rightShowValue: e.detail.value, }) this.setData({ leftShowValue: this.data.leftValue, }) } else { console.log('拖右超右边') this.setData({ leftShowValue: e.detail.value, }) this.setData({ rightShowValue: this.data.leftValue, }) } } const v = this.data.sliderWidth / this.data.Max if (v * (this.data.rightShowValue - this.data.leftShowValue) - this.data.blockSize >= 0) { this.setData({ setSliderLeftX: (v * this.data.leftShowValue) + this.data.blockSize / 2 + 'px', setSliderWidthX: v * (this.data.rightShowValue - this.data.leftShowValue) - this.data.blockSize + 'px', }) // console.log(1) } else { this.setData({ setSliderLeftX: (v * this.data.leftShowValue) + this.data.blockSize / 2 + 'px', setSliderWidthX: 0 + 'px', }) } }
登录后复制

相关推荐:

微信小程序中用Python生成二维码的两种方式

微信小程序功能实现:上滑加载下拉刷新

以上就是小程序中多滑块的实现代码的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是苹果冰棍最近收集整理的关于小程序中多滑块的实现代码的全部内容,更多相关小程序中多滑块内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部