mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
2.8 KiB
135 lines
2.8 KiB
6 years ago
|
<script>
|
||
|
// These values are bound to properties of the video
|
||
|
let time = 0;
|
||
|
let duration;
|
||
|
let paused = true;
|
||
|
|
||
|
let showControls = true;
|
||
|
let showControlsTimeout;
|
||
|
|
||
4 years ago
|
// Used to track time of last mouse down event
|
||
|
let lastMouseDown;
|
||
|
|
||
|
function handleMove(e) {
|
||
6 years ago
|
// Make the controls visible, but fade out after
|
||
|
// 2.5 seconds of inactivity
|
||
|
clearTimeout(showControlsTimeout);
|
||
2 years ago
|
showControlsTimeout = setTimeout(() => (showControls = false), 2500);
|
||
6 years ago
|
showControls = true;
|
||
|
|
||
|
if (!duration) return; // video not loaded yet
|
||
4 years ago
|
if (e.type !== 'touchmove' && !(e.buttons & 1)) return; // mouse not down
|
||
6 years ago
|
|
||
4 years ago
|
const clientX = e.type === 'touchmove' ? e.touches[0].clientX : e.clientX;
|
||
6 years ago
|
const { left, right } = this.getBoundingClientRect();
|
||
2 years ago
|
time = (duration * (clientX - left)) / (right - left);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
// we can't rely on the built-in click event, because it fires
|
||
|
// after a drag — we have to listen for clicks ourselves
|
||
6 years ago
|
function handleMousedown(e) {
|
||
4 years ago
|
lastMouseDown = new Date();
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
function handleMouseup(e) {
|
||
|
if (new Date() - lastMouseDown < 300) {
|
||
6 years ago
|
if (paused) e.target.play();
|
||
|
else e.target.pause();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function format(seconds) {
|
||
|
if (isNaN(seconds)) return '...';
|
||
|
|
||
|
const minutes = Math.floor(seconds / 60);
|
||
|
seconds = Math.floor(seconds % 60);
|
||
|
if (seconds < 10) seconds = '0' + seconds;
|
||
|
|
||
|
return `${minutes}:${seconds}`;
|
||
|
}
|
||
|
</script>
|
||
|
|
||
4 years ago
|
<h1>Caminandes: Llamigos</h1>
|
||
3 years ago
|
<p>From <a href="https://studio.blender.org/films">Blender Studio</a>. CC-BY</p>
|
||
4 years ago
|
|
||
|
<div>
|
||
|
<video
|
||
|
poster="https://sveltejs.github.io/assets/caminandes-llamigos.jpg"
|
||
|
src="https://sveltejs.github.io/assets/caminandes-llamigos.mp4"
|
||
4 years ago
|
on:mousemove={handleMove}
|
||
|
on:touchmove|preventDefault={handleMove}
|
||
4 years ago
|
on:mousedown={handleMousedown}
|
||
4 years ago
|
on:mouseup={handleMouseup}
|
||
4 years ago
|
bind:currentTime={time}
|
||
|
bind:duration
|
||
2 years ago
|
bind:paused
|
||
|
>
|
||
|
<track kind="captions" />
|
||
4 years ago
|
</video>
|
||
|
|
||
|
<div class="controls" style="opacity: {duration && showControls ? 1 : 0}">
|
||
2 years ago
|
<progress value={time / duration || 0} />
|
||
4 years ago
|
|
||
|
<div class="info">
|
||
|
<span class="time">{format(time)}</span>
|
||
|
<span>click anywhere to {paused ? 'play' : 'pause'} / drag to seek</span>
|
||
|
<span class="time">{format(duration)}</span>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
6 years ago
|
<style>
|
||
|
div {
|
||
|
position: relative;
|
||
|
}
|
||
|
|
||
|
.controls {
|
||
|
position: absolute;
|
||
|
top: 0;
|
||
|
width: 100%;
|
||
|
transition: opacity 1s;
|
||
|
}
|
||
|
|
||
|
.info {
|
||
|
display: flex;
|
||
|
width: 100%;
|
||
|
justify-content: space-between;
|
||
|
}
|
||
|
|
||
|
span {
|
||
|
padding: 0.2em 0.5em;
|
||
|
color: white;
|
||
|
text-shadow: 0 0 8px black;
|
||
|
font-size: 1.4em;
|
||
|
opacity: 0.7;
|
||
|
}
|
||
|
|
||
|
.time {
|
||
|
width: 3em;
|
||
|
}
|
||
|
|
||
2 years ago
|
.time:last-child {
|
||
|
text-align: right;
|
||
|
}
|
||
6 years ago
|
|
||
|
progress {
|
||
|
display: block;
|
||
|
width: 100%;
|
||
|
height: 10px;
|
||
|
-webkit-appearance: none;
|
||
|
appearance: none;
|
||
|
}
|
||
|
|
||
|
progress::-webkit-progress-bar {
|
||
2 years ago
|
background-color: rgba(0, 0, 0, 0.2);
|
||
6 years ago
|
}
|
||
|
|
||
|
progress::-webkit-progress-value {
|
||
2 years ago
|
background-color: rgba(255, 255, 255, 0.6);
|
||
6 years ago
|
}
|
||
|
|
||
|
video {
|
||
|
width: 100%;
|
||
|
}
|
||
4 years ago
|
</style>
|