小程序 slider 双向滑动

Love The Way You Lie 2024-04-20 08:24 132阅读 0赞

先上效果图,csdn不支持视频,这是手机录屏做成的gif,所以画质有点。。

20190920133405935.gif

直接上代码,小程序暂时不支持slider双向滑动,所以把两个slider拼起来用

  1. <view class='sliderHCon'>
  2. <view class='showMoney'>
  3. <text class='MoneyValue'>¥{
  4. {min}}</text>
  5. <text class='MoneyValue'>¥{
  6. {max}}</text>
  7. </view>
  8. <view class='twoSlider'>
  9. <view class='select-construct'>
  10. <view class='select-area'>
  11. <view class='slider-container'>
  12. <view class='leftSliderPrice sliderPrice' style='left:{
  13. {leftSliderPriceWidthX}};'>
  14. <view class='sliderView'>
  15. ¥{
  16. {slider1Value*scale}}
  17. </view>
  18. </view>
  19. <slider style='width:{
  20. {slider1W+"%"}}' class='slider-left' min='{
  21. {min}}' max='{
  22. {slider1Max}}' color='#FFAC30'
  23. selected-color='#e5e5e5' bindchanging='changing' catchtouchstart='changeStart' bindchange='changed' data-idx='1'></slider>
  24. <view class='rightSliderPrice sliderPrice' style='right:{
  25. {rightSliderPriceWidthX}};'>
  26. <view class='sliderView'>
  27. ¥{
  28. {slider2Value*scale}}
  29. </view>
  30. </view>
  31. <slider wx:if='{
  32. {!change}}' style='width:{
  33. {slider2W+"%"}}' class='slider-right' min='{
  34. {slider2Min}}' max='{
  35. {max}}' color='#e5e5e5'
  36. selected-color='#FFAC30' bindchanging='changing'catchtouchstart='changeStart' bindchange='changed' data-idx='2'></slider>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. .sliderHCon {
  43. height: 250rpx;
  44. width: 100%;
  45. margin: auto;
  46. display: flex;
  47. justify-content: center;
  48. align-items: center;
  49. flex-direction: column;
  50. }
  51. .MoneyValue {
  52. font-size: 22rpx;
  53. text-align: center;
  54. color: #666;
  55. margin-top: 15rpx;
  56. }
  57. .showMoney {
  58. display: flex;
  59. justify-content: space-between;
  60. width: 68%;
  61. margin-top: -34px;
  62. position: absolute;
  63. color: #666;
  64. font-size: 22rpx;
  65. }
  66. .showMoney text {
  67. margin-right: -67rpx;
  68. }
  69. .twoSlider {
  70. width: 93%;
  71. height:100px;
  72. display: flex;
  73. flex-direction: row;
  74. justify-content: center;
  75. align-items: center;
  76. position: relative;
  77. }
  78. .twoSlider .leftSliderPrice {
  79. position: absolute;
  80. top: -34rpx;
  81. left: 0;
  82. width: 100rpx;
  83. height: 60rpx;
  84. line-height: 40rpx;
  85. padding: 20rpx;
  86. text-align: center;
  87. color: #FFF;
  88. }
  89. .sliderPrice view {
  90. width: 100%;
  91. height: 100%;
  92. background-repeat: no-repeat;
  93. background-position: center;
  94. background-size: cover;
  95. font-size: 22rpx;
  96. }
  97. .twoSlider .rightSliderPrice {
  98. position: absolute;
  99. top: -34rpx;
  100. right: 0;
  101. width: 100rpx;
  102. height: 60rpx;
  103. line-height: 40rpx;
  104. padding: 20rpx;
  105. text-align: center;
  106. color: #FFF;
  107. }
  108. .sliderView {
  109. border: solid red 1rpx;
  110. color: red;
  111. }
  112. /* 滑动样式 */
  113. .select-construct{
  114. width: 100%;
  115. }
  116. .select-area{
  117. width: 80%;
  118. padding: 30px 10% 15px;
  119. display: flex;
  120. flex-direction: column;
  121. align-items: center;
  122. }
  123. .slider-container{
  124. width: 100%;
  125. display: flex;
  126. padding: 20px 0 10px;
  127. position: relative;
  128. }
  129. .slider-left,.slider-right{
  130. margin-right: -8rpx;
  131. }
  132. Page({
  133. /**
  134. * 页面的初始数据
  135. */
  136. data: {
  137. change: false, // 当两个slider在最右端重合时,将change设置为true,从而隐藏slider2,才能继续操作slider1
  138. max: 10000, // 两个slider所能达到的最大值
  139. min: 0, // 两个slider所能取的最小值
  140. rate: 100, // slider的最大最小值之差和100(或1000)之间的比率
  141. scale: 1, // 比例系数。页面显示值的时候,需要将slider1Value(slider2Value)乘以比例系数scale
  142. slider1Max: 10000, // slider1的最大取值
  143. slider1Value: 0, // slider1的值
  144. slider2Value: 10000, // slider2的值
  145. slider2Min: 0, // slider2的最小取值
  146. slider1W: 100, // slider1的宽度
  147. slider2W: 0, // slider2的宽度
  148. leftSliderPriceWidthX: '-1.5%',
  149. rightSliderPriceWidthX: '-21%'
  150. },
  151. // 开始滑动
  152. changeStart: function (e) {
  153. var idx = parseInt(e.currentTarget.dataset.idx)
  154. if (idx === 1) {
  155. // dW是当前操作的slider所能占据的最大宽度百分数
  156. var dW = (this.data.slider2Value - this.data.min) / this.data.rate
  157. this.setData({
  158. slider1W: dW,
  159. slider2W: 100 - dW,
  160. slider1Max: this.data.slider2Value,
  161. slider2Min: this.data.slider2Value,
  162. change: false
  163. })
  164. } else if (idx === 2) {
  165. var dw = (this.data.max - this.data.slider1Value) / this.data.rate
  166. this.setData({
  167. slider2W: dw,
  168. slider1W: 100 - dw,
  169. slider1Max: this.data.slider1Value,
  170. slider2Min: this.data.slider1Value,
  171. change: false
  172. })
  173. }
  174. },
  175. // 正在滑动
  176. changing: function (e) {
  177. var idx = parseInt(e.currentTarget.dataset.idx)
  178. var value = e.detail.value
  179. let rightSliderPriceWidthX = (this.data.max-value)/116-21
  180. let leftSliderPriceWidthX = value/116
  181. if (idx === 1) {
  182. this.setData({
  183. slider1Value: value,
  184. leftSliderPriceWidthX:leftSliderPriceWidthX+'%'
  185. })
  186. } else if (idx === 2) {
  187. this.setData({
  188. slider2Value: value,
  189. rightSliderPriceWidthX: rightSliderPriceWidthX+'%'
  190. })
  191. }
  192. },
  193. changed: function (e) {
  194. if (this.data.slider1Value === this.data.slider2Value && this.data.slider2Value === this.data.max) {
  195. this.setData({
  196. change: true
  197. })
  198. }
  199. },
  200. /**
  201. * 生命周期函数--监听页面加载
  202. */
  203. onLoad: function (options) {
  204. },
  205. /**
  206. * 生命周期函数--监听页面初次渲染完成
  207. */
  208. onReady: function () {
  209. },
  210. /**
  211. * 生命周期函数--监听页面显示
  212. */
  213. onShow: function () {
  214. },
  215. /**
  216. * 生命周期函数--监听页面隐藏
  217. */
  218. onHide: function () {
  219. },
  220. /**
  221. * 生命周期函数--监听页面卸载
  222. */
  223. onUnload: function () {
  224. },
  225. /**
  226. * 页面相关事件处理函数--监听用户下拉动作
  227. */
  228. onPullDownRefresh: function () {
  229. },
  230. /**
  231. * 页面上拉触底事件的处理函数
  232. */
  233. onReachBottom: function () {
  234. },
  235. /**
  236. * 用户点击右上角分享
  237. */
  238. onShareAppMessage: function () {
  239. }
  240. })

发表评论

表情:
评论列表 (有 0 条评论,132人围观)

还没有评论,来说两句吧...

相关阅读