Skip to content

Instantly share code, notes, and snippets.

@amowu
Created February 17, 2017 03:51
Show Gist options
  • Select an option

  • Save amowu/896e8b68f7c60ff5935ddcaf1102f6d3 to your computer and use it in GitHub Desktop.

Select an option

Save amowu/896e8b68f7c60ff5935ddcaf1102f6d3 to your computer and use it in GitHub Desktop.
Draggable mini scrolling video with RxJS
const video = document.getElementById('video');
const anchor = documant.getElementById('anchor');
const scroll$ = Rx.Observable.fromEvent(document, 'scroll');
scroll$
.map(event => anchor.getBoundingClientRect().bottom < 0)
.subscribe(isScrollOverAnchor => {
if (isScrollOverAnchor) {
video.classList.add('video-fixed');
} else {
video.classList.remove('video-fixed');
}
});
const mouseDown$ = Rx.Observable.fromEvent(video, 'mousedown');
const mouseUp$ = Rx.Observable.fromEvent(document, 'mouseup');
const mouseMove$ = Rx.Observable.fromEvent(document, 'mousemove');
mouseDown$
.filter(event => video.classList.contains('video-fixed'))
.map(event => mouseMove$.takeUntil(mouseUp$))
.concatAll()
.withLatestFrom(mouseDown$, (mouseMoveEvent, mouseDownEvent) => {
return {
x: Math.min(Math.max(mouseMoveEvent.clientX - mouseDownEvent.offsetX, 0), window.innerWidth - video.getBoundingClientRect().width),
y: Math.min(Math.max(mouseMoveEvent.clientY - mouseDownEvent.offsetY, 0), window.innerHeight - video.getBoundingClientRect().height)
};
})
.subscribe(pos => {
video.style.top = pos.y + 'px';
video.style.left = pos.x + 'px';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment