Last active
September 10, 2018 08:09
-
-
Save SerkanSipahi/c48989efa02307e1f6ee27023192c8b9 to your computer and use it in GitHub Desktop.
draggable div element with rxjs v6.3.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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