We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
const { fromEvent, map, switchMap, takeUntil } = rxjs const mousedown$ = fromEvent(document.getElementById('drag'), 'mousedown') const mousemove$ = fromEvent(document, 'mousemove') const mouseup$ = fromEvent(document, 'mouseup') mousedown$ .pipe( switchMap(downEv => { const startX = downEv.clientX const startY = downEv.clientY const originalLeft = +drag.style.left.replace('px', '') const originalTop = +drag.style.top.replace('px', '') return mousemove$.pipe( map(moveEv => { const moveX = moveEv.clientX const moveY = moveEv.clientY const deltaX = moveX - startX const deltaY = moveY - startY return { originalLeft, originalTop, deltaX, deltaY, } }), takeUntil(mouseup$), // `takeUntil` 不能移到外面,否则触发一次 `mouseup` 后整个流就终止了 ) }), ) .subscribe(({ originalLeft, originalTop, deltaX, deltaY }) => { drag.style.left = originalLeft + deltaX + 'px' drag.style.top = originalTop + deltaY + 'px' })
https://jsfiddle.net/rj5odeh6/24/
The text was updated successfully, but these errors were encountered:
No branches or pull requests
https://jsfiddle.net/rj5odeh6/24/
The text was updated successfully, but these errors were encountered: