123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- <template>
- <view class="double-slider" :style="{
- width: `calc(${sliderWidth} - ${blockSize})`,
- height: sliderHeight,//`calc(${sliderHeight}+3*${borderRadius})`,
- background: `${indicatorColor}`,
- borderRadius: `${radius}`,
- margin: `calc(${blockSize} / 2) 0`,
- }">
- <view class="active-slider" :style="{
- width: `${xWidth - activeX}px`,
- height: sliderHeight,//`calc(${sliderHeight}+3*${borderRadius})`,
- background: `${disabled || (leftBlockDisabled && rightBlockDisabled)? disabledIndicatorColor:activeColor}`,
- transform: `translateX(${activeX}px)`,
- left: `calc(${blockSize} / 2)`
- }">
- <view class="block1" :style="[getLeftBlockStyle]" @touchstart="blockStart1"
- @touchmove.stop.parevent="blockMove1">
- <label style="position: relative;line-height:28px; " :style="[getLableStyle]">{{currentMinValue}}</label>
- </view>
- <view class="block2" :style="[getRightBlockStyle]" @touchstart="blockStart2"
- @touchmove.stop.parevent="blockMove2" @touchend="isLeft = false">
- <view style="position: relative;line-height:28px;" :style="[getLableStyle]" >
- <label >{{currentMaxValue}}</label>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- props: {
- labelColor:{
- type:String,
- default:'#000000'
- },
- sliderExtendHeight:{
- type:String,
- default:'30rpx'
- },
- sliderWidth: {
- type: String,
- default: "100%"
- },
- sliderHeight: {
- type: String,
- default: "15rpx"
- },
- indicatorColor: {
- type: String,
- default: "#FF0000"//"#EFEFEF"
- },
- activeColor: {
- type: String,
- default: "skyblue"
- },
- borderStyle:{
- type: String,
- default: "solid"
- },
- borderColor:{
- type:String,
- default: "#00B962"
- },
- borderWidth:{
- type:String,
- default:'1rpx'
- },
- borderRadius:{
- type: String,
- default:'20rpx'
- },
- // style="border-radius: 25rpx;border-style:solid ; border-color: blue;border-width: 1rpx; "
- radius: {
- type: String,
- default: "20rpx"
- },
- blockSize: {
- type: String,
- default: "36rpx"
- },
- blockColor: {
- type: String,
- default: "#3489F8"
- },
- /*
- currentValue: {
- type: Array,
- default: [20, 80]
- },*/
- currentMinValue:{
- type:Number,
- default:0
- },
- currentMaxValue:{
- type:Number,
- defult:100
- },
- minValue: {
- type: Number,
- default: 0
- },
- maxValue: {
- type: Number,
- default: 100
- },
- step: {
- type: Number,
- default: 0
- },
- disabled: {
- type: Boolean,
- default: false
- },
- leftBlockDisabled: {
- type: Boolean,
- default: false
- },
- rightBlockDisabled: {
- type: Boolean,
- default: false
- },
- disabledIndicatorColor: {
- type: String,
- default: "#bababa"
- },
- disabledBlockColor: {
- type: String,
- default: "#8b8b8b"
- },
- blockStyle: {
- type: Object,
- default () {
- return {}
- }
- }
- },
- mounted() {
- console.log('mounted')
- const query = uni.createSelectorQuery().in(this);
- query.select('.double-slider').boundingClientRect(res => {
- this.width = res.width;
- const cMinP = this.currentMinValue/ this.maxValue * 100;
- const cMaxP = this.currentMaxValue / this.maxValue * 100;
- this.activeX = this.width * (cMinP * 0.01);
- this.xWidth = this.width * (cMaxP * 0.01);
-
- //this.currentMaxValue = this.currentValue[1];
- //this.currentMinValue = this.currentValue[0];
- //console.log('value '+this.currentMaxValue)
- this.minStepValue = this.currentMinValue;
- this.maxStepValue = this.maxValue * (cMaxP * 0.01);
- console.log('mounted 1')
- }).exec();
- query.select('.block1').boundingClientRect(res => {
- this.blockWidth = res.width;
- console.log('mounted2 ' + this.blockWidth)
- }).exec();
- },
- data() {
- return {
- width: 0,
- xWidth: 0,
- activeX: 0,
- blockWidth: 0, // 块宽度
- blockX1: 0,
- blockX2: 0,
- overlap: false,
- isLeft: false,
- minStepValue: 0,
- maxStepValue: 0
- }
- },
- computed: {
- /* currentMinValue(){
- return currentValue[0];
- },
- currentMaxValue(){
- return currentValue[1];
- },*/
- stepValueW() {
- const stepP = this.step / this.maxValue * 100;
- return this.width * stepP * 0.01;
- },
- getBlockStyle() {
- this.blockStyle = {
- width: this.blockSize,
- height: this.blockSize,
- ...this.blockStyle,
- }
- return this.blockStyle;
- },
- getLableStyle(){
- const styleObj = Object.assign({}, this.getBlockStyle);
- if(this.borderRadius)
- {
- styleObj.top = this.borderRadius;
- }
- if(this.labelColor){
- styleObj.color = this.labelColor;
- }
-
- return styleObj;
- },
- getLeftBlockStyle() {
- const styleObj = Object.assign({}, this.getBlockStyle);
- styleObj.background = styleObj.backgroundImage || styleObj.background ? styleObj.background :
- `${this.disabled || this.leftBlockDisabled ? this.disabledBlockColor : this.blockColor}`;
- if(this.borderStyle)
- styleObj.borderStyle = this.borderStyle;
- if(this.blockColor)
- styleObj.borderColor = this.borderColor;
- if(this.borderWidth)
- styleObj.borderWidth = this.borderWidth;
- if(this.borderRadius)
- styleObj.borderRadius = this.borderRadius;
-
- return styleObj;
- },
- getRightBlockStyle() {
- const styleObj = Object.assign({}, this.getBlockStyle);
- styleObj.background = styleObj.backgroundImage || styleObj.background ? styleObj.background :
- `${this.disabled || this.rightBlockDisabled ? this.disabledBlockColor : this.blockColor}`;
- if(this.borderStyle)
- styleObj.borderStyle = this.borderStyle;
- if(this.blockColor)
- styleObj.borderColor = this.borderColor;
- if(this.borderWidth)
- styleObj.borderWidth = this.borderWidth;
- if(this.borderRadius)
- styleObj.borderRadius = this.borderRadius;
-
- return styleObj;
- }
- },
- methods: {
- reset(e){
- console.log('e'+JSON.stringify(e))
- this.activeX = this.width * (e.minP * 0.01);
- this.xWidth = this.width * (e.maxP * 0.01);
-
- this.value[0] = e.minValue;
- this.value[1] = e.maxValue;
- },
- // 记录一个块位置
- blockStart1(e) {
- if (this.disabled || this.leftBlockDisabled) {
- return;
- }
- this.blockX1 = e.touches[0].clientX;
- },
- // 记录第二个块位置
- blockStart2(e) {
- this.blockX2 = e.touches[0].clientX;
- if (Math.round(this.activeX) == Math.round(this.xWidth)) {
- this.overlap = true;
- } else {
- this.overlap = false;
- }
- },
- // 第一个块移动
- blockMove1(e) {
- if (this.disabled || this.leftBlockDisabled) {
- return;
- }
- let x = (this.blockX1 + (e.touches[0].clientX - this.blockX1)) - this.blockWidth;
- // 如果设置了步长
- if (this.step > 0) {
- // 移动的数值
- let cValue = this.maxValue * (x / this.width * 100 * 0.01);
- if (cValue >= this.minStepValue + this.step && Math.round(this.activeX) < Math.round(this.xWidth)) {
- this.minStepValue += this.step;
- this.activeX += this.stepValueW;
- this.sendEmit();
- } else if (cValue <= this.minStepValue - this.step && Math.round(this.activeX) > 0) {
- this.minStepValue -= this.step;
- this.activeX -= this.stepValueW;
- this.sendEmit();
- }
- return;
- }
- // 如果本次移动的x值超过最大的值或小于最小值
- if (x > this.xWidth) {
- x = this.xWidth;
- } else if (x <= 0) {
- x = 0;
- }
- this.activeX = x;
- this.sendEmit();
- },
- // 第二个块移动
- blockMove2(e) {
- const activeX = this.activeX;
- let x = (this.blockX2 + (e.touches[0].clientX - this.blockX2)) - this.blockWidth;
- // 如果是重叠, 并且只对左边生效
- if (this.overlap && (x - this.step) <= this.xWidth) {
- if (this.disabled || this.leftBlockDisabled) {
- return;
- }
- this.isLeft = true;
- let cValue = this.maxValue * (x / this.width * 100 * 0.01);
- // 如果有步长
- if (this.step > 0) {
- if (cValue >= this.minStepValue + this.step && Math.round(this.activeX) < Math.round(this
- .xWidth)) {
- this.minStepValue += this.step;
- this.activeX += this.stepValueW;
- this.sendEmit();
- } else if (cValue <= this.minStepValue - this.step && Math.round(this.activeX) > 0) {
- this.minStepValue -= this.step;
- this.activeX -= this.stepValueW;
- this.sendEmit();
- }
- return;
- }
- // 移动超出设置
- if (x <= 0) {
- x = 0;
- } else if (x >= this.xWidth) { // 如果移动的值大于最大值
- x = this.xWidth;
- }
- this.activeX = x;
- this.sendEmit();
- } else {
- if (this.disabled || this.rightBlockDisabled) {
- return;
- }
- // 重叠之后,第一次拖动是往左边的话将无法超出右边
- if (this.isLeft) {
- this.activeX = this.xWidth;
- this.sendEmit();
- return;
- }
- // 不重叠正常设置
- this.overlap = false;
- // 如果设置步长
- if (this.step > 0) {
- // 移动的数值
- let cValue = this.maxValue * (x / this.width * 100 * 0.01);
- // 判断移动方向
- if (cValue >= this.maxStepValue + this.step && Math.round(this.xWidth) < Math.round(this.width)) {
- this.maxStepValue += this.step;
- this.xWidth += this.stepValueW;
- this.sendEmit();
- } else if (cValue <= this.maxStepValue - this.step && Math.round(this.xWidth) > Math.round(this
- .activeX)) {
- this.maxStepValue -= this.step;
- this.xWidth -= this.stepValueW;
- this.sendEmit();
- }
- return;
- }
- // 移动超出设置
- if (x <= this.activeX) {
- x = this.activeX;
- } else if (x >= this.width) { // 如果移动的值大于最大值
- x = this.width;
- }
- // 正常设置
- this.xWidth = x;
- }
- this.sendEmit();
- },
- calc_position(){
-
- },
- sendEmit() {
- // 最小百分比
- let minP = Math.floor(this.activeX / this.width * 100);
- // 最小值
- let minValue = Math.floor((this.maxValue * (minP * 0.01)) * 100) / 100;
- // 最大百分比
- let maxP = Math.floor(this.xWidth / this.width * 100);
- // 最大值
- let maxValue = Math.floor((this.maxValue * (maxP * 0.01)) * 100) / 100;
- this.currentMinValue = minValue;
- this.currentMaxValue = maxValue;
- //console.log('min value'+this.currentMinValue)
- //console.log('max value'+this.currentMaxValue)
-
- // console.log("最小百分比",minP);
- // console.log("最小值",minValue);
- // console.log("最大百分比",maxP);
- // console.log("最大值",maxValue);
- this.$emit("change", {
- minValue,
- maxValue,
- minP,
- maxP
- })
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .double-slider {
- margin: 0 auto;
- .active-slider {
- position: relative;
- .block1,
- .block2 {
- border-radius: 50%;
- position: absolute;
- top: 50%;
- transition: all 0.05s;
- }
- .block1 {
- left: 0;
- transform: translate(-50%, -50%);
- }
- .block2 {
- right: 0;
- transform: translate(50%, -50%);
- z-index: 1;
- }
- }
- }
- </style>
|