graceSlider.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="grace-slider" :style="{height:blockSize+'px', width:realWidth+'px'}">
  3. <view class="grace-slider-bar" :style="{background:bgColor, width:realWidth+'px', height:height+'px'}"></view>
  4. <view class="grace-slider-active-bar"
  5. :style="{background:activeColor, width:activeWidth+'px', height:height+'px', marginLeft:activeLeft+'px'}"
  6. ></view>
  7. <view class="grace-slider-block"
  8. :style="{background:blockBgColor, height:blockSize+'px', width:blockSize+'px', 'margin-left':xMin+'px'}"
  9. @touchstart.stop="touchstart"
  10. @touchmove.stop.prevent="touchmove"
  11. data-tag="min"></view>
  12. <view class="grace-slider-block"
  13. :style="{background:blockBgColor, height:blockSize+'px', width:blockSize+'px', 'margin-left':xMax+'px'}"
  14. @touchstart.stop="touchstart"
  15. @touchmove.stop.prevent="touchmove"
  16. data-tag="max"></view>
  17. </view>
  18. </template>
  19. <script>
  20. const _windowWidth = uni.getSystemInfoSync().windowWidth;
  21. export default {
  22. data() {
  23. return {
  24. realWidth : 200,
  25. realMax : 200,
  26. xMin : 0,
  27. left1 : 0,
  28. xMax : 100,
  29. currentBlock : '',
  30. minValue : 0,
  31. maxValue : 100,
  32. activeWidth : 0,
  33. activeLeft : 0
  34. };
  35. },
  36. props: {
  37. bgColor : {type : String, default : "#F6F7F8"},
  38. activeColor : {type : String, default : "#3688FF"},
  39. width : {type : Number, default : 750},
  40. height : {type : Number, default :2},
  41. blockBgColor: {type : String, default : "#FFFFFF"},
  42. blockSize : {type : Number, default :30},
  43. min : {type : Number, default : 0},
  44. minDefault : {type : Number, default : 0},
  45. max : {type : Number, default : 100},
  46. maxDefault : {type : Number, default:100}
  47. },
  48. created:function(){
  49. this.realWidth = this.upx2px(this.width);
  50. this.left1 = (_windowWidth - this.realWidth) / 2;
  51. this.realMax = this.realWidth - this.blockSize;
  52. this.setSlider();
  53. },
  54. watch:{
  55. minDefault : function(){
  56. this.setSlider();
  57. },
  58. maxDefault : function(){
  59. this.setSlider();
  60. }
  61. },
  62. methods: {
  63. setSlider : function(){
  64. this.xMin = (this.minDefault - this.min) / (this.max - this.min) * this.realMax;
  65. this.xMax = (this.maxDefault - this.min) / (this.max - this.min) * this.realMax;
  66. this.minValue = this.minDefault;
  67. this.maxValue = this.maxDefault;
  68. this.activeLeft = this.xMin + 5;
  69. this.activeWidth = this.xMax - this.xMin;
  70. },
  71. touchstart: function(e) {
  72. this.currentBlock = e.target.dataset.tag;
  73. if(this.currentBlock == 'min'){
  74. var pageX = e.pageX || e.changedTouches[0].pageX;
  75. pageX = pageX - this.left1;
  76. this.xMin = pageX;
  77. }else{
  78. var pageX = e.pageX || e.changedTouches[0].pageX;
  79. pageX = pageX - this.left1;
  80. this.xMax = pageX;
  81. }
  82. },
  83. touchmove: function(e) {
  84. this.calculate(e);
  85. },
  86. px2upx: function(px) {
  87. return (750 / _windowWidth ) * px;
  88. },
  89. upx2px : function(upx){
  90. return (_windowWidth / 750) * upx;
  91. },
  92. calculate: function(e) {
  93. var pageX = e.pageX || e.changedTouches[0].pageX;
  94. pageX = pageX - this.left1;
  95. if(this.currentBlock == 'min'){
  96. var xMin = this.xMin + (pageX - this.xMin);
  97. // 最左侧限制
  98. if(xMin < 0){xMin = 0;}
  99. // 最右侧限制
  100. if(xMin >= this.realMax){xMin = this.realMax;}
  101. this.xMin = xMin;
  102. // 计算值
  103. var value = this.xMin / this.realMax * (this.max - this.min);
  104. value = parseInt(value);
  105. value = value + this.min;
  106. this.minValue = value;
  107. }else{
  108. var xMax = this.xMax + (pageX - this.xMax);
  109. // 最左侧限制
  110. if(xMax < 0){xMax = 0;}
  111. // 最右侧限制
  112. if(xMax >= this.realMax){xMax = this.realMax;}
  113. this.xMax = xMax;
  114. // 计算值
  115. var value = this.xMax / this.realMax * (this.max - this.min);
  116. value = parseInt(value);
  117. value = value + this.min;
  118. this.maxValue = value;
  119. }
  120. // 获得2个值并传递出去
  121. if(this.maxValue >= this.minValue){
  122. this.$emit('change', [this.minValue, this.maxValue]);
  123. }else{
  124. this.$emit('change', [this.maxValue, this.minValue]);
  125. }
  126. }
  127. }
  128. };
  129. </script>
  130. <style scoped>
  131. .grace-slider{position:relative; padding:10px 0;}
  132. .grace-slider-bar{width:100%; position:absolute; top:50%; transform:translate(0, -50%);}
  133. .grace-slider-block{position:absolute; top:50%; transform:translate(0, -50%); left:0; border-radius:50%; box-shadow: 0rpx 0rpx 6rpx #ccc;}
  134. .grace-slider-active-bar{width:100%; position:absolute; top:50%; transform:translate(0, -50%);}
  135. </style>