draggable-vue-directive
draggable-vue-directive copied to clipboard
On scroll element also moves up and down?So can this feature not be used in a long page?
By element I mean draggable element
Please provide explanation, example and screenshot
See my draggable component gets attached to screen and remains there only as I scroll through the page ...for eg if I have element at centre of screen then it remains there only ..it doesn't get attached to page
Currently you can't. We are working on it
The problem is actual for me too Very sad...
@ashutosh1807 Are you fix this for you? Or what the alternative of yours solution?
I was about to open this very issue, when I found that someone else ran into the same problem.
Source of this issue
The issue is with line 163 in draggable.ts
, where the element’s position is set to fixed
.
Recommendation
-
This line of code should probably replace
fixed
withabsolute
. I think most web developers assume this would be the default kind of positioning for draggable-type functionality. -
Perhaps an
arg
parameter for the directive’sbinding
could allow client code to “opt-in” to the current behavior (via something likev-draggable:fixed
) to declare a component should have the current “scroll-ignoring” positioning.
FYI, onPositionChange
event also can be a workaround before the code itself isn't changed.
I'm using the directive like v-draggable='draggables'
and then the data and event parameter would become:
data() {
return {
draggables: {
onPositionChange: this.onPosChanged
}
};
},
methods: {
onPosChanged: function(positionDiff, absolutePosition, event) {
if(event.target.closest("[draggable-state]")){
event.target.closest("[draggable-state]").style.position = "absolute";
}
}
},
Or better;
[draggable-state]{
position: absolute !important;
}
Hi guys, first of all thanks for your ideas, they are always welcomed.
I already started to work about the solution, 'absolute' positioning is problematic with bounding-rectangle feature.
When can solve this issues?
it will take a while
@IsraelZablianov I congratulate you on the pack you created. Please fix this function.
I've ended with a solution without modifying the directive, assuming multiple boxes to drag, bounding to a container.
1) Set each box a class
.drag-box {
position: absolute !important;
}
-
Each box's initialPosition is set relative to container, e.g. { left: 0, top: 0 }
-
in onDragStart, change the box's position to 'fixed' and make sure top & left are wrt to the viewport
onDragStart: function(positionDiff, absolutePosition, event) {
let box = event && event.target && event.target.closest('[draggable-state]')
if (box) {
// Get the model
const id = box.id
let found = this.boxes.find(b => b.id === id)
if (found && !found.firstTouched) {
// On the first touch, the directive's initialPosition, startDragPosition, currentDragPosition needs to be set wrt to the viewport
const container = this.$refs.container.getBoundingClientRect()
const top = absolutePosition.top + container.top
const left = absolutePosition.left + container.left
box.style.top = top + 'px'
box.style.left = left + 'px'
let draggableState = JSON.parse(box.getAttribute('draggable-state')) || {}
draggableState.initialPosition = { left, top }
draggableState.startDragPosition = { left, top }
draggableState.currentDragPosition = { left, top }
box.setAttribute('draggable-state', JSON.stringify(draggableState))
// Record in model that the box has been touched
found.firstTouched = true
} else {
box.style.top = absolutePosition.top + 'px'
box.style.left = absolutePosition.left + 'px'
}
// Remove the class
box.classList.remove('drag-box')
}
}
- in onDragEnd, change the box's position to 'absolute' and make sure top & left are wrt to the container
onDragEnd: function(positionDiff, absolutePosition, event) {
let box = event && event.target && event.target.closest('[draggable-state]')
if (box) {
const container = this.$refs.container.getBoundingClientRect()
const top = absolutePosition.top - container.top
const left = absolutePosition.left - container.left
box.style.top = top + 'px'
box.style.left = left + 'px'
// Add the class again
box.classList.add('drag-box')
}
}
A remaining problem is if the page scrolled, the directive's initialPosition, startDragPosition, currentDragPosition are not updated and the first touch of the box will suddenly move the box to somewhere else. But after the first touch, everything is fine again.