chunLei-slider.nvue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div class="slider flex" id="slider" :style="{ width: `${width}px` }" ref="slider">
  3. <div class="slider-left flex" :style="{ width: `${leftWidth}px` }" @touchend.stop="sliderTouch">
  4. <div class="left" :style="{backgroundColor:backgroundColor}"></div>
  5. </div>
  6. <div class="slider-right flex" @touchend.stop="sliderTouch" :style="{ width: `${currentWidth-leftWidth}px` }">
  7. <div class="right"></div>
  8. </div>
  9. <div class="block flex" :style="{backgroundColor:touch?blockOuterColor:'rgba(0,0,0,0)', left: `${leftWidth}px`}"
  10. @click.stop="">
  11. <div class="block-inner flex" :style="{backgroundColor:blockBackgroundColor}" @touchmove.stop.prevent="blockTouchMove" @touchend="blockTouchEnd" @touchstart="blockTouchStart">
  12. <div class="block-inner-inner" :style="{backgroundColor:blockColor}">
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. export default{
  20. props:{
  21. min:{
  22. type:Number,
  23. default:0
  24. },
  25. max:{
  26. type:Number,
  27. default:0
  28. },
  29. value:{
  30. type:Number,
  31. default:0
  32. },
  33. width:{
  34. type:Number,
  35. default:0
  36. },
  37. ratio:{
  38. type:Number,
  39. default:1
  40. },
  41. direction:{
  42. type:String,
  43. default:'screenX'
  44. },
  45. backgroundColor:{ //滑块右侧背景条的颜色
  46. type:String,
  47. default:'#e9e9e9'
  48. },
  49. blockColor:{ //滑块颜色
  50. type:String,
  51. default:'#ffffff'
  52. },
  53. screenLeft:{ //slider距离左边距离
  54. type:Number,
  55. default:0
  56. },
  57. iosDirection:{
  58. type:Number,
  59. default:1
  60. }
  61. },
  62. data(){
  63. return{
  64. oldToucesX:0,
  65. leftWidth:0,
  66. oldLeftWidth:0,
  67. touch:false
  68. }
  69. },
  70. mounted() {
  71. },
  72. methods:{
  73. sliderTouch(e){
  74. console.log(e)
  75. let touches = e.changedTouches[0]
  76. this.leftWidth = (touches[this.direction]||touches['clientX'])*this.ratio-this.screenLeft
  77. this.leftWidth = this.leftWidth > this.currentWidth? this.currentWidth : this.leftWidth
  78. this.leftWidth = this.leftWidth < 0? 0 : this.leftWidth
  79. this.blockTouchEnd()
  80. },
  81. // 触摸开始
  82. blockTouchStart(e) {
  83. this.touch = true
  84. this.oldLeftWidth = this.leftWidth
  85. this.oldToucesX = e.changedTouches[0][this.direction]||e.changedTouches[0]['clientX'];
  86. },
  87. // 计算方向
  88. blockTouchMove(e) {
  89. let newToucesX
  90. newToucesX= e.changedTouches[0][this.direction]||e.changedTouches[0]['clientX'];
  91. this.leftWidth = this.iosDirection*(newToucesX - this.oldToucesX)*this.ratio+ this.oldLeftWidth
  92. this.leftWidth = this.leftWidth > this.currentWidth? this.currentWidth : this.leftWidth
  93. this.leftWidth = this.leftWidth < 0? 0 : this.leftWidth
  94. },
  95. // 结束触摸
  96. blockTouchEnd(e) {
  97. let current = this.leftWidth / this.currentWidth *this.max
  98. const event = {detail:{value:current}}
  99. this.$emit('change',event)
  100. this.touch = false
  101. },
  102. colorRgb(string,opacity){
  103. var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  104. var sColor = string.toLowerCase();
  105. if(sColor && reg.test(sColor)){
  106. if(sColor.length === 4){
  107. var sColorNew = "#";
  108. for(var i=1; i<4; i+=1){
  109. sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
  110. }
  111. sColor = sColorNew;
  112. }
  113. //处理六位的颜色值
  114. var sColorChange = [];
  115. for(var i=1; i<7; i+=2){
  116. sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
  117. }
  118. return "rgba(" + sColorChange.join(",") +`,${opacity}`+")";
  119. }else{
  120. return sColor;
  121. }
  122. }
  123. },
  124. computed:{
  125. blockOuterColor(){
  126. return this.colorRgb(this.blockColor,0.4)
  127. },
  128. blockBackgroundColor(){
  129. return this.colorRgb(this.blockColor,0.5)
  130. },
  131. currentWidth(){
  132. return this.width - 40
  133. }
  134. },
  135. watch:{
  136. value:{
  137. immediate:true,
  138. handler(newVal,oldVal){
  139. if(this.touch) return
  140. this.leftWidth = newVal/this.max * this.currentWidth
  141. this.leftWidth = this.leftWidth > this.currentWidth? this.currentWidth : this.leftWidth
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .flex{
  149. /* #ifndef APP-NVUE */
  150. display: flex;
  151. /* #endif */
  152. }
  153. .slider{
  154. position: relative;
  155. flex-direction: row;
  156. height: 40px;
  157. align-items: center;
  158. justify-content: center;
  159. }
  160. .slider-left{
  161. height: 40px;
  162. flex-direction: row;
  163. align-items: center;
  164. }
  165. .left{
  166. flex: 1;
  167. height: 3px;
  168. }
  169. .slider-right{
  170. height: 40px;
  171. flex-direction: row;
  172. align-items: center;
  173. }
  174. .right{
  175. height: 3px;
  176. background-color: rgba(0,0,0,0.3);
  177. flex: 1;
  178. }
  179. .block{
  180. position: absolute;
  181. height: 40px;
  182. width: 40px;
  183. border-radius: 20px;
  184. justify-content: center;
  185. align-items: center;
  186. z-index: 999999;
  187. }
  188. .block-inner{
  189. height: 20px;
  190. width: 20px;
  191. border-radius: 10px;
  192. justify-content: center;
  193. align-items: center;
  194. }
  195. .block-inner-inner{
  196. height: 10px;
  197. width: 10px;
  198. border-radius: 5px;
  199. }
  200. </style>