您好,登錄后才能下訂單哦!
小編給大家分享一下小程序中如何實現slider多滑塊,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
最近在用原生的代碼開發小程序,需要用到多滑塊的slider,但是官方的api只支持單滑塊,所以就在原來的基礎上草草的寫了一個。
html:
<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
.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
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', }) } }
以上是“小程序中如何實現slider多滑塊”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。