¶Ô±ÈÐÂÎļþ |
| | |
| | | /** |
| | | * 使ç¨bindingxæ¹æ¡å®ç°slider |
| | | * åªè½ä½¿ç¨äºnvueä¸ |
| | | */ |
| | | // å¼å
¥bindingxï¼æ¤åºç±»ä¼¼äºå¾®ä¿¡å°ç¨åºwxsï¼ç®çæ¯è®©jsè¿è¡å¨è§å¾å±ï¼åå°è§å¾å±åé»è¾å±çéä¿¡ææ |
| | | const BindingX = uni.requireNativePlugin('bindingx') |
| | | // nvueæä½domçåºï¼ç¨äºè·ådomçå°ºå¯¸ä¿¡æ¯ |
| | | const dom = uni.requireNativePlugin('dom') |
| | | // nvueä¸ç¨äºæä½å
ç´ å¨ç»çåºï¼ç±»ä¼¼äºuni.animationï¼åªä¸è¿uni.animationä¸è½ç¨äºnvue |
| | | const animation = uni.requireNativePlugin('animation') |
| | | |
| | | export default { |
| | | data() { |
| | | return { |
| | | // ä½ç§»çåç§»é |
| | | x: 0, |
| | | // æ¯å¦æ£å¨è§¦æ¸è¿ç¨ä¸ï¼ç¨äºæ è®°å¨ç»ç±»æ¯å¦æ·»å æç§»é¤ |
| | | touching: false, |
| | | changeFromInside: false |
| | | } |
| | | }, |
| | | watch: { |
| | | // çå¬vlaueçååï¼æ¤ååå¯è½æ¯ç±äºå
é¨ä¿®æ¹v-modelçå¼ï¼æè
å¤é¨ |
| | | // 仿å¡ç«¯è·åä¸ä¸ªå¼åï¼èµå¼ç»sliderçv-modelè导è´ç |
| | | value(n) { |
| | | if (!this.changeFromInside) { |
| | | this.initX() |
| | | } else { |
| | | this.changeFromInside = false |
| | | } |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.init() |
| | | }, |
| | | methods: { |
| | | init() { |
| | | // æ´æ°æ»åå°ºå¯¸ä¿¡æ¯ |
| | | this.getSliderRect().then((size) => { |
| | | this.sliderRect = size |
| | | this.initX() |
| | | }) |
| | | }, |
| | | // è·åèç¹ä¿¡æ¯ |
| | | // è·åslider尺寸 |
| | | getSliderRect() { |
| | | // è·åæ»åæ¡çå°ºå¯¸ä¿¡æ¯ |
| | | // éè¿nvueçdom模åï¼æ¥è¯¢èç¹ä¿¡æ¯ |
| | | return new Promise((resolve) => { |
| | | this.$nextTick(() => { |
| | | dom.getComponentRect(this.$refs.slider, (res) => { |
| | | resolve(res.size) |
| | | }) |
| | | }) |
| | | }) |
| | | }, |
| | | // åå§åæé®ä½ç½® |
| | | initButtonStyle({ |
| | | barStyle, |
| | | buttonWrapperStyle |
| | | }) { |
| | | this.barStyle = barStyle |
| | | this.buttonWrapperStyle = buttonWrapperStyle |
| | | }, |
| | | emitEvent(event, value) { |
| | | this.$emit(event, value || this.value) |
| | | }, |
| | | // æ»å¨å¼å§ |
| | | async onTouchStart(e) { |
| | | // if (this.disabled) return |
| | | // // 黿¢é¡µé¢æ»å¨ï¼å¯ä»¥ä¿è¯å¨æ»å¨è¿ç¨ä¸ï¼ä¸è®©é¡µé¢å¯ä»¥ä¸ä¸æ»å¨ï¼é æä¸å¥½çä½éª |
| | | // e.stopPropagation && e.stopPropagation() |
| | | // e.preventDefault && e.preventDefault() |
| | | // // æ´æ°æ»åçå°ºå¯¸ä¿¡æ¯ |
| | | // this.sliderRect = await this.getSliderRect() |
| | | // // æ è®°æ»å¨è¿ç¨ä¸è§¦æ¸ç¹çä¿¡æ¯ |
| | | // this.touchStart(e) |
| | | // this.startValue = this.format(this.value) |
| | | // this.dragStatus = 'start' |
| | | |
| | | // æ è®°æ»å¨è¿ç¨ä¸è§¦æ¸ç¹çä¿¡æ¯ |
| | | // this.touchStart(e) |
| | | }, |
| | | // å¼å§æ»å¨ |
| | | onTouchMove(e) { |
| | | // if (this.disabled) return; |
| | | // if (this.dragStatus === 'start') { |
| | | // this.$emit('drag-start') |
| | | // } |
| | | // // æ è®°å½åæ»å¨è¿ç¨ä¸ç触ç¹ä¿¡æ¯ï¼æ¤æ¹æ³å¨touch mixinä¸ |
| | | // this.touchMove(e) |
| | | // this.dragStatus = 'draging' |
| | | // const { |
| | | // width: sliderWidth |
| | | // } = this.sliderRect |
| | | // const diff = (this.deltaX / sliderWidth) * this.getRange() |
| | | // this.newValue = this.startValue + diff |
| | | // this.updateValue(this.newValue, false, true) |
| | | // è·åå
ç´ ref |
| | | // const button = this.$refs['nvue-button'].ref |
| | | // const gap = this.$refs['nvue-gap'].ref |
| | | |
| | | // animation.transition(gap, { |
| | | // styles: { |
| | | // width: `${this.startX + this.deltaX}px` |
| | | // } |
| | | // }) |
| | | // // console.log(this.startX + this.deltaX); |
| | | // animation.transition(button, { |
| | | // styles: { |
| | | // transform: `translateX(${this.startX + this.deltaX}px)` |
| | | // } |
| | | // }) |
| | | // this.barStyle = { |
| | | // width: `${this.startX + this.deltaX}px` |
| | | // } |
| | | const { |
| | | x |
| | | } = this.getTouchPoint(e) |
| | | this.buttonWrapperStyle = { |
| | | transform: `translateX(${x}px)` |
| | | } |
| | | // this.buttonWrapperStyle = { |
| | | // transform: `translateX(${this.format(this.startX + this.deltaX)}px)` |
| | | // } |
| | | }, |
| | | // onTouchEnd() { |
| | | // if (this.disabled) return; |
| | | // if (this.dragStatus === 'draging') { |
| | | // this.updateValue(this.newValue, true) |
| | | // this.$emit('drag-end'); |
| | | // } |
| | | // }, |
| | | updateValue(value, end, drag) { |
| | | value = this.format(value) |
| | | const { |
| | | width: sliderWidth |
| | | } = this.sliderRect |
| | | const width = `${((value - this.min) * sliderWidth) / this.getRange()}` |
| | | this.value = value |
| | | this.barStyle = { |
| | | width: `${width}px` |
| | | } |
| | | // console.log('width', width); |
| | | if (drag) { |
| | | this.$emit('drag', { |
| | | value |
| | | }) |
| | | } |
| | | if (end) { |
| | | this.$emit('change', value) |
| | | } |
| | | if ((drag || end)) { |
| | | this.changeFromInside = true |
| | | this.$emit('update', value) |
| | | } |
| | | }, |
| | | // ä»valueçååï¼åæ¨å¾åºxçå¼è¯¥ä¸ºå¤å° |
| | | initX() { |
| | | const { |
| | | left, |
| | | width |
| | | } = this.sliderRect |
| | | // å¾åºxçåå§åç§»å¼ï¼ä¹æä»¥éè¦è¿ä¹åï¼æ¯å 为å¨bindingXä¸ï¼è§¦æ¸æ»å¨æ¶ï¼åªè½ç弿¬æ¬¡ç§»å¨çåç§»å¼ |
| | | // èæ æ³çå¼åç¡®çååç§»å¨ç两个ç¹çåæ å¼ï¼weex纯粹为é¿éå·´å·´çKPI(é¨é¨ä¸ç»©èæ ¸)产ç©ï¼ä¹å°±è¿æ ·äº |
| | | this.x = this.value / 100 * width |
| | | // 设置移å¨çå¼ |
| | | const barStyle = { |
| | | width: `${this.x}px` |
| | | } |
| | | // æé®çåå§å¼ |
| | | const buttonWrapperStyle = { |
| | | transform: `translateX(${this.x - this.blockHeight / 2}px)` |
| | | } |
| | | this.initButtonStyle({ |
| | | barStyle, |
| | | buttonWrapperStyle |
| | | }) |
| | | }, |
| | | // ç§»å¨ç¹å æ»é¿åº¦çç¾åæ¯ï¼æ¤å¤éè¦å
é¤ä»¥stepï¼æ¯ä¸ºäºä¿è¯step大äº1æ¶ï¼æ¯å¦10ï¼é£ä¹å¨æ»å¨11,12pxè¿æ ·ç |
| | | // è·ç¦»æ¶ï¼å®é
䏿»åæ¯ä¸ä¼æ»å¨çï¼å°äº16,17pxï¼ç»è¿åèäºå
¥åï¼å°±åæäº20pxï¼è¿è¡äºä¸ä¸ä¸ªè·³å |
| | | format(value) { |
| | | return Math.round(uni.$u.range(this.min, this.max, value) / this.step) * this.step |
| | | }, |
| | | getRange() { |
| | | const { |
| | | max, |
| | | min |
| | | } = this |
| | | return max - min |
| | | } |
| | | } |
| | | } |