主要分界面與邏輯兩大塊css
界面分爲5個部分vue
邏輯git
this.rangeWidth = document.body.clientWidth
@touchstart.stop.prevent="leftTextTouchStart" //按下 @touchmove.stop.prevent="leftTextTouchMove" //滑動 @touchend.stop.prevent="leftTextTouchEnd" //鬆開//右滑塊,同上 @touchstart.stop.prevent="rightTextTouchStart" @touchmove.stop.prevent="rightTextTouchMove" @touchend.stop.prevent="rightTextTouchEnd"
//滑動事件方法 leftTextTouchStart() { this.leftClick = true; }, leftTextTouchEnd() { this.leftClick = false; }, //類樣式綁定 :class="{check_text_div:leftClick}"
rangeWidth //整個容器的寬度 leftWidth //左邊滑動的距離,經過滑動事件定義 rightWidth //右邊滑動的距離,經過滑動事件定義 width() { return Math.min(Math.max(0, this.rangeWidth - this.leftWidth - this.rightWidth), this.rangeWidth)//內容寬度應等於總寬度減去左右兩邊,且大於等於0小於等於總寬度 } left() { return Math.max(this.leftWidth, 0)//防止左滑出界面 } right() { if (this.left + this.rightWidth <= this.rangeWidth) return Math.max(this.rightWidth - 0.5, 0)//防止右滑出界面 }
leftTextTouchMove(e) { let touch = e.changedTouches[0]; let clientX = touch.clientX;//獲取滑動事件的橫座標值 if (clientX >= 0) {//只檢測滑塊在座標值在屏幕內 if (this.left + this.right <= this.rangeWidth) {//防止左右滑塊位置倒置 this.leftWidth = clientX;//左滑塊距離等於x座標 //界面操做 $('#nowRange').css({'left': this.left, 'width': this.width}); $('#leftText').css({'left': this.left}); $('#leftImg').css({'left': this.left}); } } } rightTextTouchMove(e) { let touch = e.changedTouches[0]; let clientX = touch.clientX;//獲取滑動事件的橫座標值 if (clientX <= this.rangeWidth) {//只檢測滑塊在座標值在屏幕內 this.rightWidth = this.rangeWidth - clientX;//右邊滑塊距離等於總長度減去滑動橫座標 if (this.left + this.right <= this.rangeWidth) {//防止左右滑塊位置倒置 //界面變化 $('#nowRange').css({'width': this.width}); $('#rightText').css({'right': this.right}); $('#rightImg').css({'right': this.right}); } } },
6.文本內容經過計算值即可實現github
leftText() { let num = parseInt(this.left / this.rangeWidth * 100); if (num === 0 || isNaN(num)) return '不限'; return num + 'k'; } rightText() { if (this.rangeWidth === 0) return '不限'; let num = parseInt((this.rangeWidth - this.right) / this.rangeWidth * 100); if (num >= 0) { if (num === 100) return '不限'; return num + 'k'; } }