diff --git a/examples/01-example.vue b/examples/01-example.vue index 34bd853..e132fbd 100644 --- a/examples/01-example.vue +++ b/examples/01-example.vue @@ -2,7 +2,7 @@
- Base Example + Horizontal Base Example
item1
- H +
item2
- H +
item3
item4
- H +
item5
diff --git a/examples/02-example.vue b/examples/02-example.vue index 59cdc80..51b3236 100644 --- a/examples/02-example.vue +++ b/examples/02-example.vue @@ -2,7 +2,7 @@
- Disabled Example + Horizontal Disabled Example
item1
- H +
item2
item3
- H +
item4
- H +
item5
diff --git a/examples/03-example.vue b/examples/03-example.vue index 8e3c197..2c681ba 100644 --- a/examples/03-example.vue +++ b/examples/03-example.vue @@ -2,7 +2,7 @@
- MinWdith and MaxWidth Example + Horizontal MinWdith and MaxWidth Example
-
min 100px && max 300px
- H +
min 100px && max 30rem
+
item2
- H +
item3
min 50px
- H -
min 100px
+ +
min 10em
@@ -61,7 +61,7 @@ } .zone .item.i1 { min-width: 100px; - max-width: 300px; + max-width: 30rem; background: #999; } .zone .item.i2 { @@ -75,7 +75,7 @@ background: #333; } .zone .item.i5 { - min-width: 100px; + min-width: 10em; background: #000; } diff --git a/examples/04-example.vue b/examples/04-example.vue new file mode 100644 index 0000000..6ce2195 --- /dev/null +++ b/examples/04-example.vue @@ -0,0 +1,81 @@ + + + + + diff --git a/examples/05-example.vue b/examples/05-example.vue new file mode 100644 index 0000000..46f2cf1 --- /dev/null +++ b/examples/05-example.vue @@ -0,0 +1,83 @@ + + + + + diff --git a/examples/06-example.vue b/examples/06-example.vue new file mode 100644 index 0000000..f076945 --- /dev/null +++ b/examples/06-example.vue @@ -0,0 +1,81 @@ + + + + + diff --git a/examples/index.js b/examples/index.js index 3765758..dbb79cd 100644 --- a/examples/index.js +++ b/examples/index.js @@ -1,9 +1,15 @@ import example01 from './01-example.vue' import example02 from './02-example.vue' import example03 from './03-example.vue' +import example04 from './04-example.vue' +// import example05 from './05-example.vue' +// import example06 from './06-example.vue' module.exports = { example01, example02, - example03 + example03, + example04, + // example05, + // example06 } diff --git a/handle.bak.vue b/handle.bak.vue new file mode 100644 index 0000000..526908b --- /dev/null +++ b/handle.bak.vue @@ -0,0 +1,235 @@ + + + + + diff --git a/handle.vue b/handle.vue index 44468cc..7af9176 100644 --- a/handle.vue +++ b/handle.vue @@ -1,5 +1,5 @@ @@ -12,82 +12,97 @@ mounted() { this.dragHandleBuild() }, + computed: { + horizontal() { + return Object.is(this.$parent.options.direction, 'horizontal') + } + }, methods: { dragHandleBuild() { - // self let self = this + // 模式 + let horizontal = self.horizontal + // 父容器宽高信息 let $wrap = $(this.$parent.$el) // 父容器宽度 let warpClientWidth = $wrap[0].clientWidth + let warpClientHeight = $wrap[0].clientHeight // 载体本身 - let $currentLabel = $(this.$el) + let $currentHandle = $(this.$el) // 载体宽高 - let currentLabelWidth = $currentLabel[0].clientWidth - let currentLabelHeight = $currentLabel[0].clientHeight + let currentHandleWidth = $currentHandle[0].clientWidth + let currentHandleHeight = $currentHandle[0].clientHeight // 计算元素组的指定属性之和 - let elementsWidths = (elements = [], attr = 'minWidth', notReduce = false) => { + let elementsAttrs = (elements = [], attr = 'minWidth', notReduce = false) => { if (!elements.length) return 0 - let elementsArr = Array.from(elements).map(label => { - let labelWidth = $(label).css(attr) - if (!labelWidth || labelWidth === 'auto') { - labelWidth = 0 + let elementsArr = Array.from(elements).map(element => { + let elementAttr = $(element).css(attr) + if (!elementAttr || elementAttr === 'auto') { + elementAttr = 0 } else { - labelWidth = parseInt(labelWidth) + elementAttr = parseInt(elementAttr) } - return labelWidth + return elementAttr }) return notReduce ? elementsArr : elementsArr.reduce((preValue, curValue) => { return preValue + curValue }) } // 给当前label绑定事件 - $currentLabel.bind('mousedown', function(e) { + $currentHandle.bind('mousedown', function(e) { // 判断是否禁用 if (self.disabled) return false - // 父容器左边绝对位置 + // 父容器绝对位置 let wrapOffsetLeft = $wrap.offset().left + let wrapOffsetTop = $wrap.offset().top // 要设置的属性 - let buildStyle = 'width' + let buildStyle = horizontal ? 'width' : 'height' // 获取前后的所有兄弟元素 - let $prevAll = $currentLabel.prevAll() - let $nextAll = $currentLabel.nextAll() - let $prevLabels = $prevAll.filter('.drag-handle') - let $nextLabels = $nextAll.filter('.drag-handle') - let $prevLabel = $prevLabels[0] ? $($prevLabels[0]) : null - let $nextLabel = $nextLabels[0] ? $($nextLabels[0]) : null - let prevLabelOffsetLeft = null - let nextLabelOffsetLeft = null - let prevLabelOffsetTop = null - let nextLabelOffsetTop = null - - // 获取到前后紧邻的Label的距左距离 - if ($prevLabels.length) prevLabelOffsetLeft = $($prevLabels[0]).offset().left - if ($nextLabels.length) nextLabelOffsetLeft = $($nextLabels[0]).offset().left + let $prevAll = $currentHandle.prevAll() + let $nextAll = $currentHandle.nextAll() + let $prevHandles = $prevAll.filter('.drag-handle') + let $nextHandles = $nextAll.filter('.drag-handle') + let $prevHandle = $prevHandles[0] ? $($prevHandles[0]) : null + let $nextHandle = $nextHandles[0] ? $($nextHandles[0]) : null + let prevHandleOffsetLeft = null + let nextHandleOffsetLeft = null + let prevHandleOffsetTop = null + let nextHandleOffsetTop = null + + // 获取到前后紧邻的Handle的距左/上距离 + if ($prevHandles.length) { + horizontal && (prevHandleOffsetLeft = $($prevHandles[0]).offset().left) + !horizontal && (prevHandleOffsetTop = $($prevHandles[0]).offset().top) + } + if ($nextHandles.length) { + horizontal && (nextHandleOffsetLeft = $($nextHandles[0]).offset().left) + !horizontal && (nextHandleOffsetTop = $($nextHandles[0]).offset().top) + } // 前面需要设置样式的元素集合 - let prevElementsToDo = $prevLabel ? $currentLabel.prevUntil('.drag-handle') : $prevAll + let prevElementsToDo = $prevHandle ? $currentHandle.prevUntil('.drag-handle') : $prevAll // 后面需要设置样式的元素集合 - let nextElementsToDo = $nextLabel ? $currentLabel.nextUntil('.drag-handle') : $nextAll + let nextElementsToDo = $nextHandle ? $currentHandle.nextUntil('.drag-handle') : $nextAll // 前面的元素的最小宽度之和 - let prevElementsMinWidths = elementsWidths(prevElementsToDo) + let prevElementsMinWidths = elementsAttrs(prevElementsToDo) // 前面的元素的最大宽度之和 - let prevElementsMaxWidths = elementsWidths(prevElementsToDo, 'maxWidth') + let prevElementsMaxWidths = elementsAttrs(prevElementsToDo, 'maxWidth') // 后面的元素的最小宽度之和 - let nextElementsMinWidths = elementsWidths(nextElementsToDo) + let nextElementsMinWidths = elementsAttrs(nextElementsToDo) // 监听移动 $(document).bind('mousemove', function(event) { @@ -102,7 +117,7 @@ // 如果左边有label,则应该是label的左边距加左边label的宽度 // 如果左边没有label,则应该是父容器的距左距离 // 无论什么情况,如果左边元素有min属性,则应该要加上min属性值的总和 - let minScope = ($prevLabel ? (prevLabelOffsetLeft + $prevLabel[0].clientWidth) : wrapOffsetLeft) + prevElementsMinWidths + let minScope = ($prevHandle ? (prevHandleOffsetLeft + $prevHandle[0].clientWidth) : wrapOffsetLeft) + prevElementsMinWidths // 可移动的最大范围 @@ -113,11 +128,11 @@ // - 无论上面任意情况,如果右边元素有min属性,则应该要减去这个属性值的总和 let maxScope if (!!prevElementsMaxWidths) { - maxScope = prevElementsMaxWidths + ($prevLabel ? (prevLabelOffsetLeft + $prevLabel[0].clientWidth) : wrapOffsetLeft) + maxScope = prevElementsMaxWidths + ($prevHandle ? (prevHandleOffsetLeft + $prevHandle[0].clientWidth) : wrapOffsetLeft) } else { - if (nextLabelOffsetLeft) maxScope = nextLabelOffsetLeft - if (!nextLabelOffsetLeft) maxScope = warpClientWidth + wrapOffsetLeft - maxScope -= (currentLabelWidth + nextElementsMinWidths) + if (nextHandleOffsetLeft) maxScope = nextHandleOffsetLeft + if (!nextHandleOffsetLeft) maxScope = warpClientWidth + wrapOffsetLeft + maxScope -= (currentHandleWidth + nextElementsMinWidths) } // 限制最大最小范围 @@ -127,14 +142,11 @@ // 设置左边元素的宽度 if (prevElementsToDo.length) { let toDoWidth - if (prevLabelOffsetLeft) { - toDoWidth = mouseX - prevLabelOffsetLeft - currentLabelWidth + if (prevHandleOffsetLeft) { + toDoWidth = mouseX - prevHandleOffsetLeft - currentHandleWidth } else { toDoWidth = mouseX - wrapOffsetLeft } - // 给有minwidth的全部width设置为自己的minwidth - // 给没有minwidth的设置toDoWidth - minwidth之和 / 自己数量 - // prevElementsToDo.css(buildStyle, toDoWidth / prevElementsToDo.length) // 如果toDoWidth还大于minwidth之和,则分配 if (toDoWidth > prevElementsMinWidths) { @@ -143,9 +155,9 @@ let average = toDoWidth / prevElementsToDo.length // 最大minwidth - let prevElementsMinMaxWidth = Math.max.apply(null, elementsWidths(prevElementsToDo, 'minWidth', true)) + let prevElementsMinMaxWidth = Math.max.apply(null, elementsAttrs(prevElementsToDo, 'minWidth', true)) - console.log(average, prevElementsMinMaxWidth) + // console.log(average, prevElementsMinMaxWidth) // 如果平均分配的值大于最大的minwidth,则平均分配 if (average >= prevElementsMinMaxWidth) { @@ -164,7 +176,7 @@ }) // 给没有minwidth的设置(toDoWidth - minwidth之和) / 自己数量 $(notHasMinWidthElements).css(buildStyle, (toDoWidth - prevElementsMinMaxWidth) / notHasMinWidthElements.length) - console.log($(notHasMinWidthElements)) + // console.log($(notHasMinWidthElements)) } } } @@ -173,12 +185,12 @@ if (nextElementsToDo.length) { let toDoWidth - if (nextLabelOffsetLeft) { - toDoWidth = nextLabelOffsetLeft - mouseX + if (nextHandleOffsetLeft) { + toDoWidth = nextHandleOffsetLeft - mouseX } else { toDoWidth = warpClientWidth - (mouseX - wrapOffsetLeft) } - toDoWidth -= currentLabelWidth + toDoWidth -= currentHandleWidth // 如果toDoWidth还大于minwidth之和,则分配 if (toDoWidth > nextElementsMinWidths) { @@ -187,7 +199,7 @@ let average = toDoWidth / nextElementsToDo.length // 最大minwidth - let nextElementsMinMaxWidth = Math.max.apply(null, elementsWidths(nextElementsToDo, 'minWidth', true)) + let nextElementsMinMaxWidth = Math.max.apply(null, elementsAttrs(nextElementsToDo, 'minWidth', true)) // console.log(average, nextElementsMinMaxWidth) @@ -226,9 +238,12 @@