Skip to content

Instantly share code, notes, and snippets.

@SerkanSipahi
Last active September 10, 2018 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SerkanSipahi/c48989efa02307e1f6ee27023192c8b9 to your computer and use it in GitHub Desktop.
Save SerkanSipahi/c48989efa02307e1f6ee27023192c8b9 to your computer and use it in GitHub Desktop.
draggable div element with rxjs v6.3.2
let draggableEl = document.querySelector('.draggable');
let mouseup$ = fromEvent(document, 'mouseup');
let mousemove$ = fromEvent(document, 'mousemove');
let mousedown$ = fromEvent(draggableEl, 'mousedown');
let mousedrag$ = mousedown$.pipe(
mergeMap(({offsetX, offsetY}) =>
mousemove$.pipe(
map(({clientX, clientY}) => ({
left: clientX - offsetX,
top: clientY - offsetY,
})),
takeUntil(mouseup$),
),
),
);
mousedrag$.subscribe(({top, left}) => {
draggableEl.style.top = top;
draggableEl.style.left = left;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment