Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dock and direction #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 17 additions & 7 deletions simpledrag.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
// simple drag
function sdrag(onDrag, onStop, direction) {

var names = ['left', 'top', 'right', 'bottom'];
var nameX = {true: 'left', false: 'right'};
var nameY = {true: 'top', false: 'bottom'};
var x = true;
var y = true;
var startX = 0;
var startY = 0;
var el = this;
Expand All @@ -84,7 +89,8 @@
startX = fix.startX;
}
if (false === ('skipX' in fix)) {
el.style.left = (pageX - startX) + 'px';
el.style[nameX[x]] = (pageX - startX) * (x?1:-1) + 'px';
el.style[nameX[!x]] = 'unset';
}
}
if ('horizontal' !== direction) {
Expand All @@ -93,18 +99,22 @@
startY = fix.startY;
}
if (false === ('skipY' in fix)) {
el.style.top = (pageY - startY) + 'px';
el.style[nameY[y]] = (pageY - startY) * (y?1:-1) + 'px';
el.style[nameY[!y]] = 'unset';
}
}
}

function startDragging(e) {
if (e.currentTarget instanceof HTMLElement || e.currentTarget instanceof SVGElement) {
dragging = true;
var left = el.style.left ? parseInt(el.style.left) : 0;
var top = el.style.top ? parseInt(el.style.top) : 0;
startX = e.pageX - left;
startY = e.pageY - top;
var style = getComputedStyle(el);
var vs = {}
names.forEach(n => vs[n] = style[n] ? parseInt(style[n]) : 0);
x = vs.left < vs.right;
y = vs.top < vs.bottom;
startX = e.pageX - vs[nameX[x]] * (x?1:-1);
startY = e.pageY - vs[nameY[y]] * (y?1:-1);
window.addEventListener('mousemove', move);
}
else {
Expand All @@ -123,4 +133,4 @@
}

Element.prototype.sdrag = sdrag;
})();
})();