line-and-surface/app/src/components/VideoScroll.vue

109 lines
2.3 KiB
Vue
Raw Normal View History

2021-01-09 20:36:20 +01:00
<template>
2021-01-09 20:53:56 +01:00
<div class="video-scroll" ref="root">
2021-01-09 20:36:20 +01:00
<img v-for="(file, idx) in definition.files"
2021-01-09 20:53:56 +01:00
:data-src="file"
2021-01-09 20:36:20 +01:00
:style="{
top: `${Math.round(definition.top)}px`,
left: `${Math.round(definition.left) + width * idx}px`,
}"
/>
</div>
</template>
<script lang="ts">
2021-01-09 20:53:56 +01:00
import {defineComponent, onMounted, ref} from "vue";
2021-01-09 20:36:20 +01:00
export default defineComponent({
name: "VideoScroll",
props: {
definition: {
type: Object,
required: true
}
},
setup(props) {
const width = ref(0);
const height = ref(0);
2021-01-09 20:53:56 +01:00
const root = ref<Element | null>(null);
2021-01-09 20:36:20 +01:00
const definition = props.definition as VideoScrollDef;
getMeta(definition.files[0]).then((img) => {
width.value = img.width;
height.value = img.height;
});
2021-01-09 20:53:56 +01:00
onMounted(() => {
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
2021-01-10 00:00:42 +01:00
const element = entry.target as HTMLImageElement;
if (entry.isIntersecting) {
2021-01-10 00:00:42 +01:00
element.classList.add("visible");
if (!element.src) {
element.src = element.dataset.src!;
element.onload = () => {
element.classList.add("loaded");
};
}
} else {
element.classList.remove("visible");
2021-01-09 20:53:56 +01:00
}
});
2021-01-09 20:53:56 +01:00
}, {rootMargin: "100px"});
Array.from((root.value as Element).children).forEach((el) => {
observer.observe(el);
});
});
2021-01-09 20:36:20 +01:00
return {
2021-01-09 20:53:56 +01:00
root,
2021-01-09 20:36:20 +01:00
width,
height
};
}
});
export enum VideoScrollDirection {
RIGHT
}
export interface VideoScrollDef {
top: number,
left: number,
direction: VideoScrollDirection,
files: string[],
style?: { [key: string]: string }
}
function getMeta(url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
let img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject();
img.src = url;
});
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.video-scroll img {
position: absolute;
image-rendering: optimizeSpeed;
2021-01-09 23:56:14 +01:00
opacity: 0;
transition: opacity .5s;
2021-01-10 00:00:42 +01:00
visibility: hidden;
2021-01-09 23:56:14 +01:00
}
2021-01-10 00:00:42 +01:00
.loaded {
2021-01-09 23:56:14 +01:00
opacity: 1 !important;
2021-01-09 20:36:20 +01:00
}
2021-01-10 00:00:42 +01:00
.visible {
visibility: visible !important;
}
2021-01-09 20:36:20 +01:00
</style>