|
| 1 | +<template> |
| 2 | + <div class="g-resize-bar"> |
| 3 | + <div class="g-resize-highlight" :style="{width: left + '%',}"></div> |
| 4 | + <button class="circle-btn" @touchstart.self="drag" @mousedown.self="drag" :style="{left: left + '%',}"></button> |
| 5 | + </div> |
| 6 | +</template> |
| 7 | + |
| 8 | +<style scoped> |
| 9 | + .g-resize-bar{ |
| 10 | + position: absolute; |
| 11 | + margin: 17px auto; |
| 12 | + height: 6px; |
| 13 | + border-radius: 3px; |
| 14 | + width:200px; |
| 15 | + margin-left: -100px; |
| 16 | + left: 50%; |
| 17 | + background-color: #ddd; |
| 18 | + } |
| 19 | + .g-resize-highlight{ |
| 20 | + position: absolute; |
| 21 | + left: 0; |
| 22 | + top:0; |
| 23 | + height: 6px; |
| 24 | + background-color: #27ae60; |
| 25 | + border-radius: 3px; |
| 26 | + } |
| 27 | + .circle-btn{ |
| 28 | + display: block; |
| 29 | + position: absolute; |
| 30 | + left:0; |
| 31 | + top: -5px; |
| 32 | + width: 14px; |
| 33 | + height: 14px; |
| 34 | + margin-left: -7px; |
| 35 | + background-color: #fff; |
| 36 | + border-radius: 7px; |
| 37 | + box-shadow: 0 2px 4px -2px rgba(0,0,0,.5), 0 -2px 4px -2px rgba(0,0,0,.45); |
| 38 | + border-width: 0; |
| 39 | + } |
| 40 | +
|
| 41 | + @media all and (max-width:480px) { |
| 42 | + .g-resize-bar{ |
| 43 | + width:120px; |
| 44 | + margin-left: auto; |
| 45 | + left:10px; |
| 46 | + } |
| 47 | +
|
| 48 | + } |
| 49 | +</style> |
| 50 | + |
| 51 | +<script> |
| 52 | +import helper from './lib/helper'; |
| 53 | +import drag from './lib/drag'; |
| 54 | +
|
| 55 | +export default { |
| 56 | + props: { |
| 57 | + minProgress: { |
| 58 | + type:[Number, String], |
| 59 | + default: 0, |
| 60 | + } |
| 61 | +
|
| 62 | + }, |
| 63 | + data() { |
| 64 | + return { |
| 65 | + progress: 100, |
| 66 | + left: 100, |
| 67 | + } |
| 68 | + }, |
| 69 | +
|
| 70 | + methods: { |
| 71 | + setRatio(num) { |
| 72 | + this.progress = 100; |
| 73 | + this.left = 100; |
| 74 | + }, |
| 75 | +
|
| 76 | + drag(e) { |
| 77 | + e.preventDefault(); |
| 78 | + const $el = e.target; |
| 79 | + this.el = $el; |
| 80 | + const $container = this.$el.parentElement; |
| 81 | + const self = this; |
| 82 | + const isMobile = helper.isMobile; |
| 83 | + const width = document.querySelector('.g-resize-bar').offsetWidth; |
| 84 | + const coor = { |
| 85 | + x: (isMobile ? e.touches[0]['clientX'] : e.clientX) - $el.offsetLeft - $el.parentElement.offsetLeft, |
| 86 | + y: (isMobile ? e.touches[0]['clientY'] : e.clientY) - $el.offsetTop - $el.parentElement.offsetTop, |
| 87 | + posX: isMobile ? e.touches[0]['clientX'] : e.clientX, |
| 88 | + posy: isMobile ? e.touches[0]['clientY'] : e.clientY, |
| 89 | + maxLeft: width, |
| 90 | + maxTop: parseInt($container.style.height) - parseInt($el.style.height), |
| 91 | + }; |
| 92 | + const move = function (ev) { |
| 93 | + const newCoor = drag(ev, self.el, coor); |
| 94 | + if (newCoor) { |
| 95 | + console.log(self.minProgress); |
| 96 | + if((newCoor.left / width) < self.minProgress) { |
| 97 | + return; |
| 98 | + } |
| 99 | + self.progress = newCoor.left / width; |
| 100 | + self.left = newCoor.left / width * 100; |
| 101 | +
|
| 102 | + self.$emit('resize', self.progress); |
| 103 | + } |
| 104 | + }; |
| 105 | + const stopMove = function (ev) { |
| 106 | + self.el = null; |
| 107 | + if (isMobile) { |
| 108 | + document.removeEventListener('touchmove', move, false); |
| 109 | + document.removeEventListener('touchend', stopMove, false); |
| 110 | + return; |
| 111 | + } |
| 112 | + document.removeEventListener('mousemove', move, false); |
| 113 | + document.removeEventListener('mouseup', stopMove, false); |
| 114 | + }; |
| 115 | + if (isMobile) { |
| 116 | + document.addEventListener('touchmove', move, false); |
| 117 | + document.addEventListener('touchend', stopMove, false); |
| 118 | + return; |
| 119 | + } |
| 120 | + document.addEventListener('mousemove', move, false); |
| 121 | + document.addEventListener('mouseup', stopMove, false); |
| 122 | + } |
| 123 | +
|
| 124 | + }, |
| 125 | +
|
| 126 | +
|
| 127 | +}; |
| 128 | +</script> |
0 commit comments