Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div id="app">
<h3>v-drag - make any element draggable</h3>
<div class="demo-outer">
<div class="block" v-drag>
<div class="block" v-drag.window-only>
Drag me
</div>
</div>
Expand Down
22 changes: 14 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ export function mouseup (e, el, _data) {
setDraggerOffset(el, _data)
}

function reachedLeft(el, _data, movingLeft) {
return (el.offsetLeft + _data.width >= window.innerWidth) && !movingLeft
function reachedLeft(el, _data) {
return el.offsetLeft <= 0
}

function reachedRight(el, _data, movingRight) {
return el.offsetLeft <= 0 && !movingRight
function reachedRight(el, _data) {
return el.offsetLeft + _data.width >= window.innerWidth
}

function reachedTop(el, _data, movingUp) {
Expand All @@ -107,14 +107,20 @@ function reachedBottom(el, _data, movingDown) {

export function mousemove (e, el, _data) {
if (_data.down) {
const movingLeft = _data.cursorPreviousX > e.clientX
const movingRight = _data.cursorPreviousX < e.clientX
const movingRight = _data.cursorPreviousX > e.clientX
const movingLeft = _data.cursorPreviousX < e.clientX
const movingUp = _data.cursorPreviousY < e.clientY
const movingDown = _data.cursorPreviousY > e.clientY

if (_data.constraintToWindow && (reachedLeft(el, _data, movingLeft) || reachedRight(el, _data, movingRight))) {
const atLeft = reachedLeft(el, _data)
const atRight = reachedRight(el, _data)
if (_data.constraintToWindow && (atLeft || atRight)) {
if (atRight) el.style.left = window.innerWidth - _data.width + 'px'
if (atLeft) el.style.left = 0

// do now allow moving outside the window horizontally
} else {
else if (movingLeft && (x.clientX < window.innerWidth - _data.width))
// } else {
el.style.left = _data.draggerOffsetLeft + (e.clientX - _data.initialX) + 'px'
}
if (_data.constraintToWindow && (reachedTop(el, _data, movingUp) || reachedBottom(el, _data, movingDown))) {
Expand Down