JSFiddle - React, Tailwind, and code Playground
by Jerry Hong
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.1/Rx.js"></script>
<div id="anchor">
<div class="video" id="video">
<div class="masker"></div>
<video width="100%" controls>
<source src="http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_stereo.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
</div>
</div>
SCSS
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
html, body {
margin: 0;
padding: 0;
height: 2000px;
background-color: tomato;
}
#anchor {
height: 360px;
width: 100%;
background-color: #F0F0F0;
}
.video {
width: 640px;
height: 360px;
margin: 0 auto;
background-color: black;
&.video-fixed {
position: fixed;
top: 10px;
left: 10px;
width: 320px;
height: 150px;
cursor: all-scroll;
.masker {
display: none;
}
&:hover {
.masker {
display: block;
position: absolute;
width: 100%;
height: 180px;
background-color: rgba(0, 0, 0, 0.8);
z-index: 2;
}
}
}
}
JavaScript
const video = document.getElementById('video');
const anchor = document.getElementById('anchor');
const scroll = Rx.Observable.fromEvent(document, 'scroll');
const mouseDown = Rx.Observable.fromEvent(video, 'mousedown')
const mouseUp = Rx.Observable.fromEvent(document, 'mouseup')
const mouseMove = Rx.Observable.fromEvent(document, 'mousemove')
const validValue = (value, max, min) => {
return Math.min(Math.max(value, min), max)
}
scroll
.map(e => anchor.getBoundingClientRect().bottom < 0)
.subscribe(bool => {
if(bool) {
video.classList.add('video-fixed');
} else {
video.classList.remove('video-fixed');
}
})
mouseDown
.filter(e => video.classList.contains('video-fixed'))
.map(e => mouseMove.takeUntil(mouseUp))
.concatAll()
.withLatestFrom(mouseDown, (move, down) => {
return {
x: validValue(move.clientX - down.offsetX, window.innerWidth - 320, 0),
y: validValue(move.clientY - down.offsetY, window.innerHeight - 180, 0)
}
})
.subscribe(pos => {
video.style.top = pos.y + 'px';
video.style.left = pos.x + 'px';
})